Negated character classes

By placing the carrot character (^) in the first position inside a character class (just next to [) negates the matching of the character class. A negated character class matches any character that is not included in the class. A negated character class also negates a character range by matching any character not matched by the character range.

You learned in Chapter 1, Getting Started with Regular Expressions, that dot (.) matches any character except the newline character. However, note that the negated character class also matches newline characters such as \r, \n, and so on.

Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.