How to iterate over regex expression

You can validate and iterate over matches with one regex by:

  • Ensuring there are no unmatched characters between matches (e.g. name1=x;;name2=y;) by putting a \G at the start of our regex, which mean “the end of the previous match”.

  • Checking whether we’ve reached the end of the string on our last match by comparing the length of our string to Matcher.end(), which returns the offset after the last character matched.

Something like:

String line = "name1=gil;name2=orit;";
Pattern p = Pattern.compile("\\G(\\w+)=(\\w+);");
Matcher m = p.matcher(line);
int lastMatchPos = 0;
while (m.find()) {
   System.out.println(m.group(1));
   System.out.println(m.group(2));
   lastMatchPos = m.end();
}
if (lastMatchPos != line.length())
   System.out.println("Invalid string!");

Live demo.

Leave a Comment