I am getting a text from the DB which contains Strings of the form
CO<sub>2</sub>
In order to recognize this I wrote the following code
String footText = "... some text containing CO<sub>2</sub>";
String co2HTML = "CO<sub>2</sub>";
Pattern pat = Pattern.compile(co2HTML);
Matcher mat = pat.matcher(footText);
final boolean hasCO2 = mat.matches();
The problem is that hasCO2 is always false although the inout text has that substring.
What is wrong hete?
Thanks!
You should use
find()instead ofmatches(), since the latter tries to match the entire string against the pattern rather than perform a search.From the Javadoc:
Also, the pattern in question doesn’t really require regular expressions; you could use
String.indexOf()to perform the search.