What's the regular expression that matches a square bracket?
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?
How about using backslash \
in front of the square bracket. Normally square brackets match a character class.
Try using \\[
, or simply \[
.
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):
Are you escaping it with \
?
/\[/
Here's a helpful resource to get started with Regular Expressions:
Regular-Expressions.info
In general, when you need a character that is "special" in regexes, just prefix it with a \
. So a literal [
would be \[
.