Examples of negated character classes

The following regex matches any character except a forward slash:

[^/]

Also, remember the fact that a negated character such as [^/] must match a single character. It doesn't match zero-width assertions such as ^, $, \Z, \z, \b, \B, and so on.

The following regex matches any character but a and A:

    [^aA]

The following regex matches all the consonants of the English language:

    [^aeiou]

All non-vowels are considered consonants; hence, we just need to negate the vowel character class.

The following regex matches all the characters except digits, dots, and line breaks:

    [^0-9.\r\n]

In this regex, we could also use the predefined property \d for [0-9]:

    [^\d.\r\n]

This regex matches http followed by ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.