Friday, November 11, 2011

Regular expressions simplify pattern-matching code - 7


·  Intersection: consists of characters common to all nested classes and matches only common characters. Example: [a-z&&[d-f]] matches characters d, e, and f. The following command line offers a second example:
java RegexDemo [aeiouy&&[y]] party

java RegexDemo [aeiouy&&[y]] party matches y in [aeiou&&[y]] with y in party. No other matches exist.
·  Subtraction: consists of all characters except for those indicated in nested negation character classes and matches the remaining characters. Example: [a-z&&[^m-p]] matches characters a through l and q through z. The following command line offers a second example:
java RegexDemo [a-f&&[^a-c]&&[^e]] abcdefg

java RegexDemo [a-f&&[^a-c]&&[^e]] abcdefg matches d and f with their counterparts in abcdefg. No other matches exist.

No comments: