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.
-
23In case you are trying to write this regex in C# you have have to use \\ in front of the square bracket. Jan 25, 2011 at 5:26
-
7Actually I don't know where it works and why did the answer receive such a high rank. Dec 1, 2012 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.– tripleeeMar 1, 2021 at 10:40
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):
-
can you tell us about it? The roof
^
meansnot
in square bracket that is a character class.– TimoMar 30, 2021 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. Sep 14, 2017 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
– PysisApr 27, 2022 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 \[
.
If you want to remove the [
or the ]
, use the expression: "\\[|\\]"
.
The two backslashes escape the square bracket and the pipe is an "or".
-
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"]
– dkoJan 11, 2021 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.– tripleeeMar 1, 2021 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.
-
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).– mguttMay 27 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: