Sunteți pe pagina 1din 1

Regular Expressions Cheat Sheet

by DaveChild via cheatography.com/1/cs/5/


Regular Expressions Anchors

Regular Expressions Quantifiers

Regular Expressions Groups and Ranges

Start of string, or start of line in multi-line pattern

0 or more

Any character except new line (\n)

\A

Start of string

1 or more

(a|b)

a or b

End of string, or end of line in multi-line pattern

0 or 1

(...)

Group

\Z

End of string

{3}

Exactly 3

(?:...)

Passive (non-capturing) group

\b

Word boundary

{3,}

3 or more

[abc]

Range (a or b or c)

\B

Not word boundary

{3,5}

3, 4 or 5

[^abc]

Not a or b or c

\<

Start of word

Add a ? to a quantifier to make it ungreedy.

[a-q]

Letter from a to q

\>

End of word

[A-Q]

Upper case letter from A to Q

Regular Expression Common Metacharacters

[0-7]

Digit from 0 to 7

Regular Expressions Character Classes

\n

nth group/subpattern

\c

Control character

Ranges are inclusive.

\s

White space

\S

Not white space

\d

Digit

<

>

\D

Not digit

\w

Word

\W

Not word

Regular Expressions Special Characters

\x

Hexadecimal digit

\n

New line

\O

Octal digit

\r
\t
\v

Vertical tab

Regular Expressions POSIX

Regular Expressions Pattern Modifiers


g

Global match

Case-insensitive

Multiple lines

Treat string as single line

Allow comments and white space in pattern

Carriage return

Evaluate replacement

Tab

Ungreedy pattern

The escape character is usually the backslash - \.

[:upper:]

Upper case letters

\f

Form feed

Regular Expressions String Replacement

[:lower:]

Lower case letters

\xxx

Octal character xxx

$n

nth non-passive group

[:alpha:]

All letters

\xhh

Hex character hh

$2

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

[:alnum:]

Digits and letters

$1

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

[:digit:]

Digits

$`

Before matched string

[:xdigit:]

Hexadecimal digits

$'

After matched string

[:punct:]

Punctuation

$+

Last matched string

[:blank:]

Space and tab

$&

Entire matched string

[:space:]

Blank characters

Some regex implementations use \ instead of $.

[:cntrl:]

Control characters

[:graph:]

Printed characters

[:print:]

Printed characters and spaces

[:word:]

Digits, letters and underscore

Regular Expressions Assertions


?=

Lookahead assertion

?!

Negative lookahead

?<=

Lookbehind assertion

?!= or ?<!

Negative lookbehind

?>

Once-only Subexpression

?()

Condition [if then]

?()|

Condition [if then else]

?#

Comment

Cheatographer

Cheat Sheet

Sponsor

DaveChild

This cheat sheet was published on 19th October,

Envoy, for simple and effective bug management.

cheatography.com/davechild/

2011 and was last updated on 20th October, 2011.

Try it free!

www.addedbytes.com/

http://www.envoyapp.com

S-ar putea să vă placă și