Why is my string not match()ing?
I have a multiline XML blob in a Java String that I'd like to grep some values out of. I know that regexes and XML mix like crude oil and pasta, but I promise that I'm really not trying anything complicated.
Sample XML object
String:
<ObjectDto>
<created>1313825589244</created>
<description>description</description>
<id>649</id>
<isFoo>true</isFoo>
<ObjectDto>
All I want is for object.matches("<isFoo>true</isFoo>")
to evaluate to true
. I've tried wrapping the meat of the regex in .*
(and [\n.]*
in case the newlines were t开发者_开发问答hrowing it), but no joy.
Why doesn't it match?
I wasn't matching newlines hard enough, turning on DOTALL (?s).*<isFoo>true</isFoo>.*
fixed it.
Because you haven't read the documentation.
Attempts to match the entire input sequence against the pattern.
If the match succeeds then more information can be obtained via the start, end, and group methods.
You should either add a non-greedy dot operator at the start and end or just use the find()
method.
精彩评论