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

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.