Regular Expressions: An Introduction
by Amit Arora
Published: January 12, 2004
User Rating: 8.9 (282 votes)

Page Page » 1 2 3 4 5 6 7 8 9

Special Characters

Ok, we have done few examples and now lets go an check out few special characters that have special meaning in the pattern. We 'll be taking them section by section.

Escape Character

Back Slash (\): Back Slash (\) is treated as the escape charater in the the regex patterns. Black Slash (\) and the character followed by it, together have special meaning in the pattern. Depending on what is after the slash, different, the combination has different meaning.

In the regexp, the characters dot (.), multiply (*), back slash (\) have special meaning. What is we wanted to include these special characters into the match itself ? What if we wanted to match a file name which contains both the back slash (\) and a dot (.). How would we do that ? Simple, just escape them. i.e. add a back slash (\) before them. Similary, you can escape, other special characters (which we haven't discussed)

\\ Back Slash Match the character (\) \* Multiply Match the character (*) \. Dot Match the character (.)

However, if the slash is followed by any one of the following, it has a special meaning. I have taken the few absolutely necessay combinations here. For more combinations you can read through the references.

\s White Space Match white space ( ) \d Decimal Match the decimals 0-9 \D Not Decimal Match any characters except decimals 0-9 \w Word Match a word character (includes alphabets, numbers and underscore) \W Not Word Match a character that is not a word character

Taking a few examples

Regex /C:\\/ Explaintaion Match "C:\" Regex /C:\\Program\sFiles/ Explaintaion Match "C:\Program Files" Regex /C:\\Program\sFiles\\\d{1}\.txt/ Explaintaion Match "C:\Program Files\", followed by a decimal number, followed by ".txt" Note How the slashes (\\), white space(\s) and dot (\.) have been escaped in the pattern Example C:\Program Files\1.txt
Page Page » 1 2 3 4 5 6 7 8 9


Amit Arora is web developer with expertise in developing eCommerce enabled websites for the businesses.

Contact | Resume

Subscribe to newsletter

Get Firefox

Monitored by Site24x7