Anchors

Quantifiers

Groups and Ranges

^

\A

Start of string

*

+

0 or more

1 or more

.

Any character ex

new line (\n)

cept

$

End of string

?

0 or 1

(a|b)

a or b

\Z

End of string

{3}

Exactly 3

(...)

Group

\b

Word boundary

{3,}

3 or more

(?:...)

Passive Group

\B

Not word boundary

{3,5}

3, 4 or 5

[abc]

Range (a or b or c)

\<

Start of word

[^abc]

Not a or b or c

\>

End of word

[a-q]

Letter between a and q

[A-Q]

Upper case letter Quantifier Modifiers between A and Q

"x" below represents a quantifier x?

Ungreedy version of "x"

[0-7]

Digit between 0 and 7

Character Classes

\n

nth group/subpattern Note: Ranges are inclusive.

\c

\s

Control char

White spaceacter

\S

Not white space

Escape Character

\d

Digit

\D

Not digit

\

Escape Character

Pattern Modifiers

\w

Word

\W

Not word

g

i

Global match

Case-insensitive

Metacharacters (must be escaped)

\x

Hexadecimal digit m

Multiple lines

\O

Octal digit

s

Treat string as single line

^

$

[{

.*

x

Allow comments and (

\

+

white space in pattern

)

|

?

e

Evaluate replacement POSIX

<

>

U

Ungreedy pattern

[:upper:]

[:lower:]

Upper case letters Lower case letters

[:alpha:]

All letters

[:alnum:]

Digits and letters Special Characters String Replacement (Backreferences)

[:digit:]

Digits

[:xdigit:]

Hexadecimal digits

\n

\r

New line

Carriage return

$n

$2

nth non-passive group

"xyz" in /^(abc(xyz))$/

[:punct:]

Punctuation

\t

Tab

$1

"xyz" in /^(?:abc)(xyz)$/

[:blank:]

Space and tab

\v

Vertical tab

$`

Before matched string

[:space:]

Blank characters

\f

Form feed

$'

After matched string

[:cntrl:]

Control characters

\xxx

Octal character xxx $+

Last matched string

[:graph:]

Printed characters

\xhh

Hex character hh

$&

Entire matched string

[:print:]

Printed characters and spaces

[:word:]

Digits, letters and Sample Patterns

underscore

Pattern

([A-Za-z0-9-]+)

Will Match

Letters, numbers and hyphens (\d{1,2}\/\d{1,2}\/\d{4}) Date (e.g. 21/3/2006) Assertions

([^\s]+(?=\.(jpg|gif|png))\.\2) jpg, gif or png image (^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^50$) Any number from 1 to 50 inclusive

?=

?!

Lookahead assertion Negative lookahead (#?([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?) Valid hexadecimal colour code

?<=

Lookbehind assertion ((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}) String with at least one upper case

?!= or ?<!

Negative lookbehind letter, one lower case letter, and one

?>

Once-only Subexpression digit (useful for passwords).

?()

Condition [if then]

(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}) Email addresses

?()|

Condition [if then else]

(\<(/?[^\>]+)\>) HTML Tags

?#

Comment

Note: These patterns are intended for reference purposes and have not been extensively tested. Please use with caution and test thoroughly before use.

Available free from www.ILoveJackDaniels.com