Memorize this: . ^ $ * + ? ( ) [ { | [backslash]

.^$*+?()[{|\

This is a really good set of symbols to memorize because if you are writing a Perl-compatible regular expression (PCRE) then these are the symbols which if they occur as literals within expressions (and outside of character classes), they need to be escaped. [Sorry WordPress is messing up the backslash character in the title]

PCRE’s are used in languages including PHP, Python, and Java. And of course they are used in Perl.

Remember that character classes are enclosed in square brackets [], for example:
[a-z0-9] is a character class consisting of the range of lowercase letters and digits 0 through 9.

The symbols which must be escaped inside character classes are intuitive so don’t really need to be memorized. They include – which is used for ranges (as above), the \ character itself, and the right square bracket ] as well as the caret ^ which negates or inverts a character class.

Reference & credit:
stackoverflow.com: What special characters must be escaped in regular expressions?


Comments

Leave a Reply