regex - Java - Reg expression using groups -
After one string, I need to exit groups that match a given pattern. An example string: Each group will start with The product is & lt; XmlLrvs & gt; First & lt; / XmlLrvs & gt; & Lt; XmlLrvs & gt; Second & lt; / XmlLrvs & gt; & Lt; XmlLrvs & gt; Third & lt; / XmlLrvs & gt;
& lt; XmlLrvs & gt;
and & lt; / XmlLrvs & gt; Ends with
. Here is my code ...
string is a piece of patternStr = "(; XmlLrvs & gt;. & Lt; & lt / XmlLrvs & gt;);"; // compile and use regular expressions pattern pattern = Pattern.compile (patternStr); Matcher matcher = pattern.matcher (text); Matcher.matches (); Go to all groups for this match for // (I int = 1; I & lt; = matcher.groupCount (); i ++) {println (matcher.group (i)); }
& lt; XmlLrvs & gt; Third & lt; / XmlLrvs & gt;
. I expect the first and the second group but they are not being captured. Can anyone help?
When you should play again on matches, then you are running on groups Mail () < / Code> The method examines the entire input for a match. What you want is
find ()
method.
Change
matcher.matches (); For (int i = 1; i & lt; = matcher.groupCount (); i ++) {System.out.println (matcher.group (i)); }
to
while (matcher.find ()) {println (matcher.group (1)); }
Comments
Post a Comment