Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the regular expression that matches a square bracket?

Tags:

regex

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?

like image 644
Alfre2 Avatar asked May 29 '09 20:05

Alfre2


People also ask

What is square bracket in regular expression?

Square brackets match something that you kind of don't know about a string you're looking for. If you are searching for a name in a string but you're not sure of the exact name you could use instead of that letter a square bracket. Everything you put inside these brackets are alternatives in place of one character.

What's the difference between () and [] in regular expression?

[] denotes a character class. () denotes a capturing group. (a-z0-9) -- Explicit capture of a-z0-9 . No ranges.

How do you use square brackets in regex python?

[] - Square brackets Here, [abc] will match if the string you are trying to match contains any of the a , b or c . You can also specify a range of characters using - inside square brackets. [a-e] is the same as [abcde] . [1-4] is the same as [1234] .

How do you match a regular expression?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


1 Answers

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

like image 112
Peter Stuifzand Avatar answered Dec 21 '22 10:12

Peter Stuifzand