regex - Java - Reg expression using groups -


"itemprop =" text ">

After one string, I need to exit groups that match a given pattern.

An example string: & lt; XmlLrvs & gt; First & lt; / XmlLrvs & gt; & Lt; XmlLrvs & gt; Second & lt; / XmlLrvs & gt; & Lt; XmlLrvs & gt; Third & lt; / XmlLrvs & gt;

Each group will start with & 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)); }  

The product is & 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

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -