Character classes
We sometimes limit those characters that produce matches to a specific set of characters. For example, we might search text for vowelsa
, e
, i
, o
, and u
, where any occurrence of any vowel indicates a match. A character class, a regex construct that identifies a set of characters between open and close square bracket metacharacters ([ ]
), helps us accomplish that task. Pattern
supports the following character classes: - Simple: consists of characters placed side by side and matches only those characters. Example:
[abc]
matches charactersa
,b
, andc
. The following command line offers a second example:
· java RegexDemo [csw] cave
java RegexDemo [csw] cave
matches c
in [csw]
with c
in cave
. No other matches exist. · Negation: begins with the ^ metacharacter and matches only those characters not in that class. Example: [^abc] matches all characters except a, b, and c. The following command line offers a second example:
java RegexDemo [^csw] cave
java RegexDemo [^csw] cave matches a, v, and e with their counterparts in cave. No other matches exist.
No comments:
Post a Comment