Saturday, November 19, 2011

Regular expressions simplify pattern-matching code - 15


Embedded flag expressions resemble capturing groups because both regex constructs surround their characters with parentheses metacharacters. Unlike a capturing group, an embedded flag expression does not capture a match's characters. Thus, an embedded flag expression is an example of a noncapturing group, that is, a regex construct that does not capture text characters; it's specified as a character sequence surrounded by parentheses metacharacters. Several kinds of noncapturing groups appear in Pattern's SDK documentation.
Tip
To specify multiple embedded flag expressions in a regex, either place them side by side (e.g., (?m)(?i)) or place their lowercase letters side by side (e.g., (?mi)).

Explore the java.util.regex classes' methods

java.util.regex's three classes offer various methods to help us write more robust regex source code and create powerful tools to manipulate text. Our exploration of those methods begins in the Pattern class.
Note
You might also want to explore the CharSequence interface's methods, which you can implement when you create a new character sequence class. The only classes currently implementing CharSequence are java.nio.CharBuffer, String, and StringBuffer.

No comments: