I have a large array with string. Now, I need to use the string in the array to form patterns. However for the string with round brackets, the constructed patterns don\'t work.
It should definitely work well... if you test against a matching String :)
Your problem is that "Student (male): John"
starts with an uppercase S
, and you're trying to match a lowercase s
. That's as simple as it gets!
Note that you may use [()]
to match either (
or )
:
p = p.replaceAll("[()]", "\\\\$0");
By the way, I would also point out that you could replace the lines:
p = p.replaceAll("\\(", "\\\\(");
p = p.replaceAll("\\)", "\\\\)");
Simply by using:
p = Pattern.quote(p);
Cheers!