120

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,051
  • 11
  • 52
  • 72
Alfre2
  • 1,899
  • 3
  • 17
  • 21

8 Answers8

130

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

HamZa
  • 14,051
  • 11
  • 52
  • 72
Peter Stuifzand
  • 4,928
  • 1
  • 21
  • 28
  • 22
    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
  • 5
    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
  • 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
56

Try using \\[, or simply \[.

Alan Moore
  • 71,299
  • 12
  • 93
  • 154
dfa
  • 111,277
  • 30
  • 187
  • 226
28

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,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
13

Are you escaping it with \?

/\[/

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

Regular-Expressions.info

HamZa
  • 14,051
  • 11
  • 52
  • 72
matpie
  • 16,516
  • 9
  • 60
  • 81
8

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,051
  • 11
  • 52
  • 72
Jaytop
  • 121
  • 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
  • 25,850
  • 9
  • 83
  • 104
4

does it work with an antislash before the [ ?

\[ or \\[ ?

HamZa
  • 14,051
  • 11
  • 52
  • 72
Pierre
  • 33,089
  • 29
  • 109
  • 185
3

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
  • 71,299
  • 12
  • 93
  • 154
  • 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