I have a string stored in a variable which contains square brackets. I want to use this string as part of a regex statement, and have found I need to double escape the square brackets to get the result I need.
The string is of the form:
String str = "myVar[100]";
and the regex is used to parse a text file and look for the value between quotes after this string, i.e. the value of the variable:
if(str.contains("[")) {
str.replace("[", "\\\\[");
str.replace("]", "\\\\]");
}
Regex:
Pattern q = Pattern.compile(str + " *?= *?\"(.*)\";");
Matcher n = q.matcher(line);
while (n.find()) {
System.out.println(n.group(0));
}
But it's not finding any matches. Am I escaping the backslashes correctly?