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?
10 Answers
How about using backslash \
in front of the square bracket. Normally square brackets match a character class.

- 14,671
- 11
- 54
- 75

- 5,084
- 1
- 23
- 28
-
23In 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
-
7Actually 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
-
2The 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,455
- 1
- 17
- 27
-
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 \[
.

- 26,504
- 11
- 85
- 105
If you want to remove the [
or the ]
, use the expression: "\\[|\\]"
.
The two backslashes escape the square bracket and the pipe is an "or".

- 73,866
- 12
- 100
- 156

- 49
- 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
-
1In 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
For a pure exgex, it's simple:
1 /[]abcde]/ - it's the way to include the ']' in the class.
2 /[abc[de]/ - freely put anything else inside the brackets, including the '['. (Most of the meta-characters lose their special meaning inside '[]').
3 Test(verify) your regex w/ 'grep' or 'vim' etc. first.(They are easy-going guys.)
4 It's not too late to try inserting '\' at this moment if your scripting environment doesn't agree.

- 343
- 2
- 7
-
Really nice trick. I tried everything to include square brackets with `find -regex` and your solution was the only one that worked: `-not -regex "[][a-zA-Z0-9/.() ßÄÖÜäöüàéèí_-]+"` (this even uses the trick to include hyphen without escaping by placing it as last character in the class). – mgutt May 27 '23 at 15:55
The below expression is able to detect the timing 65
\[(?<timeElpsed>\d+)ms\]
from the below log:
[2022-12-16T04:51:55.993+0000] [ INFO] [scala-execution-context-global-75] [200 OK]: [100.107.99.132] [65ms] "content-length:

- 18,766
- 20
- 94
- 101