mercredi 13 mai 2015

JAVA + How can I replace string containing '(' with '\\(' to for pattern.compile regular expression

I have a the following code

StringToSearchIn ="Hello word (Figure 1)"; 
patternString= "word (Figure 1)" 
Pattern pattern = Pattern.compile(patternString);       
Matcher matcher = pattern.matcher(StringToSearchIn);
matcher.find()

this returning 0, as '( or )' is not recognized, when i change to patternString= "word \(Figure 1\)" , is returning the match counter as 1, is there a way to change my patternString to search for '(' and replace with '\(' dynamically before creating a pattern. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire