Wednesday, November 9, 2011

Regular expressions simplify pattern-matching code - 5


Character classes

We sometimes limit those characters that produce matches to a specific set of characters. For example, we might search text for vowels a, 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 characters a, b, and c. 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: