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 escaping rules inside the character class

The following regex matches a string containing one or more of the ap.9 characters:

    ^[ap9.]+$ 

The dot (.) doesn't need to be escaped inside the character class.

The following regex matches a string containing one or more of the @#$%.* characters:

    ^[$#@%.*]+$

None of the preceding special characters require escaping inside the character class.

The following regex matches a string containing one or more of the ?*+. characters:

    ^[*+?.]+$ 

The following regex matches an input that allows any digit, ], or ^ in the input:

    ^[\^\]0-9]+$ 

We can also write our regex as ^[\]0-9^]+$ by moving ^ away from the first position and avoiding the escaping.

The following regex matches an input that ...

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