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 allows any alphanumeric character, hyphen, dot, or backslash, such as xyzTR-4.50:

    ^[a-zA-Z0-9.-\]+$

We can also write our regex as follows by moving - to the last position inside the character class, thus avoiding the escaping:

    ^[a-zA-Z0-9.\-]+$
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.71.211