Saturday, March 13, 2010

St-st-stuttering rr-rr-regex

Credit for this one goes to Alan Moore on (again!) from his answer to my question:
Matcher m = Pattern.compile("(?=(..))").matcher("12345");
while (m.find()) {
} // 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