How do I write a Java regular expression that will find the character '[' or ']' in the expression? Since these are special characters in regular expressions, I'm stumped.
Asked
Active
Viewed 144 times
-5
-
http://stackoverflow.com/questions/14134558/java-regex-list-of-all-special-characters-that-needs-to-be-escaped-in-regex – AntonH Aug 12 '14 at 16:31
-
You need to escape it using \. Try `[\[\]]` – Braj Aug 12 '14 at 16:31
-
2You escape them. `\\[` `\\]`. Please read [Regex: Special Characters](http://www.regular-expressions.info/characters.html) – hwnd Aug 12 '14 at 16:32
-
http://ideone.com/ySMBqk – hwnd Aug 12 '14 at 16:38
-
@AntonH I had read through both of these and neither have helped me, but thanks anyway. – KSK Aug 12 '14 at 16:49
-
@hwnd I had read through both of these and neither have helped me, but thanks anyway. – KSK Aug 12 '14 at 16:50
2 Answers
0
Other than escaping, as that can get a little confusing I suppose, you can use the hex values of the characters:
[ = 5B
] = 5D
\x5B // For use in your expression

dan.john.daly
- 96
- 7