Pcre Cheat Sheet



Regular expressions python cheat sheet

  1. Php Regex Cheat Sheet
  2. Pcre Regex Cheat Sheet


Regular Expressions Cheatsheet (PCRE)

Regular Expression Basics

.

Any character except newline

a

The character a

ab

The string ab

a|b

a or b

a*

0 or more a's

Escapes a special character

Regular Expression Quantifiers

*

0 or more

+

1 or more

?

0 or 1

{2}

Exactly 2

{2, 5}

Between 2 and 5

{2,}

2 or more

Default is greedy. Append ? for reluctant.

Regular Expression Groups

(...)

Capturing group

(?P<Y>...)

Capturing group named Y

(?:...)

Non-capturing group

(?>...)

Atomic group

(?|...)

Duplicate group numbers

Y

Match the Y'th captured group

(?P=Y)

Match the named group Y

(?R)

Recurse into entire pattern

(?Y)

Recurse into numbered group Y

(?&Y)

Recurse into named group Y

g{Y}

Match the named or numbered group Y

g<Y>

Recurse into named or numbered group Y

(?#...)

Comment

Regular Expression Character Classes

[ab-d]

One character of: a, b, c, d

[^ab-d]

One character except: a, b, c, d

[b]

Backspace character

d

One digit

D

One non-digit

s

One whitespace

S

One non-whitespace

w

One word character

W

One non-word character

Regular Expression Assertions

^

Start of string

A

Start of string, ignores m flag

$

End of string

Z

End of string, ignores m flag

b

Word boundary

B

Non-word boundary

G

Start of match

(?=...)

Positive lookahead

(?!...)

Negative lookahead

(?<=...)

Positive look-behind

(?<!...)

Negative look-behind

(?()|)

Conditional

Regular Expression Escapes

Q..E

Remove special meaning

Regular Expression Flags

-i

Ignore case

m

^ and $ match start and end of line

s

. matches newline as well

x

Allow spaces and comments

J

Duplicate group names allowed

U

Ungreedy quantifiers

(?iLmsux)

Set flags within regex

Regular Expression Special Characters

n

Newline

r

Carriage return

t

Tab

0

Null character

YYY

Octal character YYY

xYY

Hexadecimal character YY

x{YY}

Hexadecimal character YY

cY

Control character Y

Regular Expression Posix Classes

[[:alnum:]]

Letters and digits

[[:alpha:]]

Letters

[[:ascii:]]

ASCII codes 0 - 127

[[:blank:]]

Space or tab only

[[:cntrl:]]

Control characters

[[:digit:]]

Decimal digits

[[:graph:]]

Visible characters, except space

[[:lower:]]

Lowercase letters

[[:print:]]

Visible characters

[[:punct:]]

Visible punctuation characters

[[:space:]]

Whitespace

[[:upper:]]

Uppercase letters

[[:word:]]

Word characters

[[:xdigit:]]

Hexadecimal digits

Expressions

Php Regex Cheat Sheet

This guide is based on the information provided by

Php regex cheat sheet. A simple PHP regex cheat sheet via, PHP PCRE Cheat Sheet. Functions pregmatch(pattern, subject, submatches) pregmatchall(pattern, subject, submatches) pregreplace(pattern PHP PCRE Cheat Sheet Functions pregmatch(pattern, subject, submatches) replacement as PHP code S Extra analysis of pattern U Pattern is ungreedy.

Last modified: Tuesday, January 24, 2017

Copyright © 2010-2017 OpenSight Software, LLC

  • Title: PHP PCRE Cheat Sheet Author: Richard Heyes Created Date: 9/14/2005 9:45:10 PM.
  • PHP PCRE Cheat Sheet. Functions Base Character Classes pregmatch(pattern, subject, submatches) w Any “word” character (a-z 0-9 ) pregmatchall(pattern, subject, submatches) W Any non “word” character pregreplace(pattern, replacement, subject) s Whitespace (space, tab CRLF) S Any non whitepsace character pregreplacecallback(pattern, callback, subject) d Digits (0-9) preg.

Although similar to Perl, PCRE has some syntax that can bedifficult to remember. When I'm working with CFEngine I often have tolook it up. Here is a cheat-sheet to save time.

DescriptionSyntaxNote
Case insensitive(?i)Place at the beginning of the expression.
Class[ ]d and [0-9-] are equivalent.
DigitdD for anything that is not a digit.
Lookahead, negative(?!*pattern*
Lookahead, positive(?=*pattern*
Lookbehind, negative(?<!*pattern*
Lookbehind, positive(?<=*pattern*
None capture grouping(?:)Group for logic and selection, but not capture.
Multi-line match(?m)Place at the beginning of the expression. Similar to Perl's m//g.
Extened regex(mxs)Use this to make your regexes readable. A regex best practice.
No magicQ ENo special meaning to any characters between these.
Range{n,m}Minimum of *n*, maximum of *m*.
WhitespacesS for anything that is not whitespace.

Notes

  1. Beginning settings are combined. For example (?mi) considersmultiple lines and is case insensitive.

  2. Make your regexes readable by using the extened regex options.Consider this:

    (?i)(?:linux|solaris|aix|hpux)

    Versus this:

    (?imsx)(?: linux | solaris | aix | hpux )

Pcre Regex Cheat Sheet

Now imagine using it on a very complex regex.See here fora more complex example.