Menu
Active1 year, 6 months ago
I want a regex that matches a square bracket
[
. I haven't found one yet. I think I tried all possibilities, but haven't found the right one. What is a valid regex for this?Regular expressions are used when you want to search for specify lines of text containing a particular pattern. Most of the UNIX utilities operate. Search for Files in Windows by RegEx? Ask Question Asked 2 years, 8 months ago. Browse other questions tagged windows search regex file-search or ask your own question. How to search for files in Windows 7+ like you could in Windows XP?
HamZa12.4k1010 gold badges4747 silver badges7070 bronze badges
Alfre2Alfre288422 gold badges1212 silver badges2020 bronze badges
8 Answers
How about using backslash
HamZa
in front of the square bracket. Normally square brackets match a character class.12.4k1010 gold badges4747 silver badges7070 bronze badges
Peter StuifzandPeter Stuifzand
Alan Moore63.3k1010 gold badges8383 silver badges139139 bronze badges
dfadfa96.9k2828 gold badges177177 silver badges219219 bronze badges
Are you escaping it with
?Here's a helpful resource to get started with Regular Expressions:
HamZa12.4k1010 gold badges4747 silver badges7070 bronze badges
matpiematpie11.8k88 gold badges5151 silver badges7777 bronze badges
In general, when you need a character that is 'special' in regexes, just prefix it with a
ZifreZifre
. So a literal [
would be [
.19.6k88 gold badges7676 silver badges101101 bronze badges
If you're looking to find both variations of the square brackets at the same time, you can use the following pattern which defines a range of either the
HamZa[
sign or the ]
sign: /[[]]/
12.4k1010 gold badges4747 silver badges7070 bronze badges
JaytopJaytop
If you want to match an expression starting with
Matt RoyMatt Roy[
and ending with ]
, use [[^]]*]
.88111 gold badge1111 silver badges2323 bronze badges
does it work with an antislash before the
[
?[
or [
?12.4k1010 gold badges4747 silver badges7070 bronze badges
PierrePierre
24.7k2424 gold badges9494 silver badges170170 bronze badges
If you want to remove the
[
or the ]
, use the expression: '[|]'
.The two backslashes escape the square bracket and the pipe is an 'or'.
Alan Moore63.3k1010 gold badges8383 silver badges139139 bronze badges
Manish KumarManish Kumar
protected by Community♦Mar 21 '18 at 20:00
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?