132

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?

HamZa
  • 14,671
  • 11
  • 54
  • 75
Alfre2
  • 2,039
  • 3
  • 19
  • 22

10 Answers10

147

How about using backslash \ in front of the square bracket. Normally square brackets match a character class.

HamZa
  • 14,671
  • 11
  • 54
  • 75
Peter Stuifzand
  • 5,084
  • 1
  • 23
  • 28
  • 23
    In 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
  • 7
    Actually 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
  • 2
    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
65

Try using \\[, or simply \[.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
dfa
  • 114,442
  • 31
  • 189
  • 228
37

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): enter image description here

Matt Roy
  • 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
13

Are you escaping it with \?

/\[/

Here's a helpful resource to get started with Regular Expressions:

Regular-Expressions.info

HamZa
  • 14,671
  • 11
  • 54
  • 75
matpie
  • 17,033
  • 9
  • 61
  • 82
9

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: /[\[\]]/

HamZa
  • 14,671
  • 11
  • 54
  • 75
Jaytop
  • 131
  • 1
  • 1
  • 3
    You 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
7

In general, when you need a character that is "special" in regexes, just prefix it with a \. So a literal [ would be \[.

Zifre
  • 26,504
  • 11
  • 85
  • 105
4

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 Moore
  • 73,866
  • 12
  • 100
  • 156
  • 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
  • 1
    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
3

does it work with an antislash before the [ ?

\[ or \\[ ?

HamZa
  • 14,671
  • 11
  • 54
  • 75
Pierre
  • 34,472
  • 31
  • 113
  • 192
2

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.

Charles Jie
  • 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
-1

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:

Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101