Thursday, November 10, 2011

Regular expressions simplify pattern-matching code - 6


·  Range: consists of all characters beginning with the character on the left of a hyphen metacharacter (-) and ending with the character on the right of the hyphen metacharacter, matching only those characters in that range. Example: [a-z] matches all lowercase alphabetic characters. The following command line offers a second example:
java RegexDemo [a-c] clown

java RegexDemo [a-c] clown matches c in [a-c] with c in clown. No other matches exist.

·  Tip
Combine multiple ranges within the same range character class by placing them side by side. Example: [a-zA-Z] matches all lowercase and uppercase alphabetic characters.

·  Union: consists of multiple nested character classes and matches all characters that belong to the resulting union. Example: [a-d[m-p]] matches characters a through d and m through p. The following command line offers a second example:
java RegexDemo [ab[c-e]] abcdef

java RegexDemo [ab[c-e]] abcdef matches a, b, c, d, and e with their counterparts in abcdef. No other matches exist.

No comments: