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?
8 Answers
How about using backslash \ in front of the square bracket. Normally square brackets match a character class.
- 14,051
- 11
- 52
- 72
- 4,928
- 1
- 21
- 28
-
22In case you are trying to write this regex in C# you have have to use \\ in front of the square bracket. – Shrewdroid Jan 25 '11 at 05:26
-
5Actually I don't know where it works and why did the answer receive such a high rank. – Vitali Pom Dec 01 '12 at 12:57
-
The question asks about regular expressions, not about how to encode them in a host language which hijacks the backslash for its own use. If you are in a place where a shell or language parser will parse or otherwise process backslashes, you probably need to double the backslash, but that's not what this specific question is asking about. – tripleee Mar 01 '21 at 10:40
If you want to match an expression starting with [ and ending with ], use \[[^\]]*\].
Here is the meaning of each part (as explained at www.regexr.com):
- 1,305
- 1
- 16
- 26
-
can you tell us about it? The roof `^` means `not` in square bracket that is a character class. – Timo Mar 30 '21 at 18:38
Are you escaping it with \?
/\[/
Here's a helpful resource to get started with Regular Expressions:
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 [ sign or the ] sign: /[\[\]]/
-
3You can omit the first backslash. `[[\]]` will match either bracket. In some regex dialects (e.g. grep) you can omit the backslash before the `]` if you place it immediately after the `[` (because an empty character class would never be useful): `[][]`. But that doesn't work in Java or JavaScript. – cayhorstmann Sep 14 '17 at 16:24
-
I was trying `sed -r 's|[\[\]]|!|g'`, but that was only matching the entire phrase once. I wanted to keep my expression explicit, but this was the only efficient combination that worked for me: `s|[][]|!|g` – Pysis Apr 27 '22 at 19:00
In general, when you need a character that is "special" in regexes, just prefix it with a \. So a literal [ would be \[.
- 25,850
- 9
- 83
- 104
If you want to remove the [ or the ], use the expression: "\\[|\\]".
The two backslashes escape the square bracket and the pipe is an "or".
- 71,299
- 12
- 93
- 154
- 39
- 1
-
Thank you!! None of the other answers worked for me. I was trying to split a json array string into a Java array.. So I had to remove the quotes and square brackets, then split on the comma `["item1" , "item2"]` – dko Jan 11 '21 at 17:14
-
In the general case, the two backslashes are wrong here. In some languages (like Java, Python if not in a literal `r"..."` string, etc) you need to backslash the backslash to pass it through to the regex engine as a single backslash, but that's a (mis)feature of the host language, and not a correct answer for a question asking simply about regular expressions without a specific host language. – tripleee Mar 01 '21 at 10:38