Saturday, 1 November 2014

Escape sequence

Escape sequence is useful to match a character that is a metacharacter.

For Example, if you want to check IP address. If you specify a regular expression like below.
\d{3}.\d{3}.\d{3}.\d{3}

Sample Output
Enter Regular Expression
\d{3}.\d{3}.\d{3}.\d{3}
Enter the string
123.456.789.012
I found the text 123.456.789.012 starting at index 0 Ending at index 15

Enter the string
1234567890987654
I found the text 123456789098765 starting at index 0 Ending at index 15

As you observe the above output, in addition to matching the IP addresses, given regular expression also match '1234567890987654', since '.' can match any character.

Now update the regular expression using escape sequence '\' like below.

\d{3}\.\d{3}\.\d{3}\.\d{3}

Sample Output
Enter Regular Expression
\d{3}\.\d{3}\.\d{3}\.\d{3}
Enter the string
123.456.789.091
I found the text 123.456.789.091 starting at index 0 Ending at index 15

Enter the string
1234567890987654





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment