![]() |
The Java Developers Almanac 1.4 |
|
e434. Adding Comments to a Regular ExpressionBy default, whitespace in a regular expression is significant. By specifying theCOMMENTS flag, whitespace can be ignored in a pattern.
While in comment mode, a particular whitespace character can be
matched using a character class (for example, [\ ] matches a space).
In comment mode, the It is also possible to enable comments within a pattern using
the inline modifier The inline modifier can also contain pattern characters using
the form CharSequence inputStr = "a b";
String patternStr = "a b";
// Compile without comments
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.matches(); // true
// Compile with comments
pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
matcher = pattern.matcher(inputStr);
matchFound = matcher.matches(); // false
// Use COMMENTS but include a character class with a space
patternStr = "a [\\ ] b";
pattern = Pattern.compile(patternStr, Pattern.COMMENTS);
matcher = pattern.matcher(inputStr);
matchFound = matcher.matches(); // true
// Use an inline modifier
matchFound = pattern.matches("a b", inputStr); // true
matchFound = pattern.matches("(?x)a b", inputStr); // false
matchFound = pattern.matches("(?x)a [\\ ] b", inputStr); // true
matchFound = pattern.matches("(?x)a \\s b", inputStr); // true
matchFound = pattern.matches("a (?x: b )", inputStr); // true
// Tabs and newlines in the pattern are ignored as well
matchFound = pattern.matches("(?x)a \t\n \\s b", inputStr); // true
// Read pattern from file
try {
File f = new File("pattern.txt");
FileReader rd = new FileReader(f);
char[] buf = new char[(int)f.length()];
rd.read(buf);
patternStr = new String(buf);
matcher = pattern.matcher(inputStr);
matchFound = matcher.matches(); // true
} catch (IOException e) {
}
Here are the contents of pattern.txt:
# This pattern matches ``a b''
a # this is the first part
[\ ] # this is the second part
b # this is the third part
e435. Compiling a Pattern with Multiple Flags
© 2002 Addison-Wesley. |