Java Reference
In-Depth Information
The following snippet of code compiles a regular expression into a Pattern object:
// Prepare a regular expression
String regex = "[a-z]@.";
// Compile the regular expression into a Pattern object
Pattern p = Pattern.compile(regex);
The second version of the compile() method lets you specify flags that modify the way the pattern is matched.
The flags parameter is a bit mask. The flags are defines as int constants in the Pattern class, as listed in Table 14-3 .
Table 14-3. List of Flags Defined in the Pattern Class
Flag
Description
Pattern.CANON_EQ
Enables canonical equivalence. If this flag is set, two characters match only if their
full canonical decompositions match.
Pattern.CASE_INSENSITIVE
Enables case-insensitive matching. This flag sets the case-insensitive matching
only for US-ASCII charset. For case-insensitive matching of Unicode charset, the
UNICODE_CASE flag should also be set in conjunction with this flag.
Pattern.COMMENTS
Permits whitespace and comments in pattern. When this flag is set, whitespace is
ignored and embedded comments starting with # are ignored until the end of a
line.
Pattern.DOTALL
Enables dotall mode. By default, the expression . does not match line terminators.
When this flag is set, the expression matches any character, including a line
terminator.
Pattern.LITERAL
Enables literal parsing of the pattern. When this flag is set, the characters in
the regular expression are treated literally. That is, metacharacters and escape
sequences have no special meanings. CASE_INSENSTIVE and UNICODE_CASE
flags retain their effects when used with this flag.
Pattern.MULTILINE
Enables multiline mode. By default, the expressions ^ and $ match the beginning
and the end of the entire input sequence. When this flag is set, they match
just after and just before a line terminator or the end of the input sequence,
respectively.
Pattern.UNICODE_CASE
Enables Unicode-aware case folding. When this flag is set in conjunction with the
CASE_INSENSITIVE flag, the case-insensitive matching is performed according to
the Unicode Standard.
Pattern.UNICODE_
CHARACTER_CLASS
Enables the Unicode version of predefined character classes and POSIX character
classes. Setting this flag also has the effect of setting the UNICODE_CASE flag.
When this flag is set, the (US-ASCII only) Predefined character classes and POSIX
character classes are in conformance with Unicode Technical Standard #18:
Unicode Regular Expression Annex C: Compatibility Properties.
Pattern.UNIX_LINES
Enables Unix lines mode. When this flag is set, only the \n character is recognized
as a line terminator.
Search WWH ::




Custom Search