A little food for thought

Thursday, June 04, 2009

Regular Expressions Syntax summary

Items to match a single character

Meta characters

Matches

.

Dot

Matches any one character

[….]

Character class

Matches any one character listed

[^….]

Negated character class

Matches any one character not listed

\char

Escaped character

Matches literal character

Items appended to provide “Counting” : The Quantifiers

?

question

One allowed but it is optional

*

star

Any number allowed but all are optional

+

Plus

At least one is required; additional are optional

{min, max}

Specified range

Min required , Max allowed

Items that match a position

^

Caret

Matches the position at the start of the line

$

Dollar

Matches the position at the end of the line

\<

Word boundary

Matches the position at the start of the word

\>

Word boundary

Matches the position at the end of the word

|

Alternation

Matches either expression it separates

( )

Parentheses

Limits the scope of alternation, provides the grouping for quantifier and captures for back references

\1 \2

Back references

Matches the text previously match within first , second etc.

\t

Tab character

 

\n

Newline character

 

\r

Carriage return

 

\s

 

Matches any whitespace

\S

 

Any thing not \s

\w

 

Matches any word

\W

 

Matches non word [^a-zA-Z0-9_]

\d

 

Matches any numeric digit [0-9]

\D

 

Any thing not \d

(?<=…)

Look behind

Successful if can match to the left

(?<!...)

Negative look behind

Successful if can not match to the left

(?=…..)

Look ahead

Success full if can match to the right

(?!....)

Negative look ahead

Successful if can not match to the right

No comments: