Saturday, March 13, 2010

St-st-stuttering rr-rr-regex

Credit for this one goes to Alan Moore on stackoverflow.com (again!) from his answer to my question:
Matcher m = Pattern.compile("(?=(..))").matcher("12345");
while (m.find()) {
   System.out.println(m.group(1));
} // prints "12", "23", "34", "45"
This is the most instructive pattern I've seen BY FAR. The most important revelations for me are:
  • You can capture during lookaround! Which means that...
  • Group zero is not necessarily a superstring of all the other groups!

No comments:

Post a Comment