O'Reilly logo

Java 9 Regular Expressions by Anubhava Srivastava

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required