Regular Expressions - Quick Reference Guide
Anchors                              Literal Characters
^
                                                                                                     Character group contents
        start of line
$       end of line                  Letters and digits match exactly             axB70              x          individual chars
b      word boundary                Some special characters match exactly        @-=%               x-y        character range
B      not at word boundary                                                                         [:class:]  posix char class
A      start of subject
                                     Escape other specials with backslash         .  $ [
G      first match in subject
                                                                                                     [^:class:] negated class
z      end of subject               Character Groups
Z      end of subject               Almost any character (usually not newline)   .                  Examples
        or before newline at end                                                                     [a-zA-Z0-9_]
                                     Lists and ranges of characters               [ ]
Non-printing characters              Any character except those listed            [^ ]               [[:alnum:]_]
a        alarm (BEL, hex 07)
cx       "control-x"
e        escape (hex 1B)            Counts (add ? for non-greedy)                                   Comments
f        formfeed (hex 0C)          0 or more ("perhaps some")                     *                (?#comment)
n        newline (hex 0A)           0 or 1 ("perhaps a")                           ?
r        carriage return (hex OD)
t                                   1 or more ("some")                             +                Conditional subpatterns
          tab (hex 09)
ddd      octal code ddd             Between "n" and "m" of                         {n,m}            (?(condition)yes-pattern)
xhh      hex code hh                Exactly "n", "n" or more                       {n}, {n,}        (?(condition)yes|no-pattern)
x{hhh..} hex code hhh..

Generic character types              Alternation                                                     Recursive patterns
d      decimal digit                Either/or                                       |               (?n)         Numbered
D      not a decimal digit                                                                          (?0) (?R)    Entire regex
s      whitespace character
S                                   Lookahead and Lookbehind                                        (?&name)     Named
        not a whitespace char
w      "word" character             Followed by                                    (?= )
W      "non-word" character         NOT followed by                                (?! )            Replacements
POSIX character classes              Following                                    (?<= )             $n   reference capture
alnum letters and digits             NOT following                                (?<! )
alpha  letters                                                                                       Case foldings
ascii  character codes 0-127
blank                                Grouping                                                        u   upper case next char
       space or tab only
cntrl  control characters            For capture and counts                       ( )                U   upper case following
digit  decimal digits                Non-capturing                                (?: )              l   lower case next char
graph  printing chars -space                                                                         L   lower case following
lower  lower case letters
                                     Named captures                               (?<name>       )
print  printing chars +space         Alternation                                                     E   end case folding
punct  printing chars -alnum         Back references
space white space                                                                                    Conditional insertions
upper
                                     Numbered                                     n gn g{n}
       upper case letters                                                                            (?n:insertion)
word   "word" characters             Relative                                     g{-n}
xdigit hexadecimal digits            Named                                        k<name>           (?n:insertion:otherwise)
                                                                                                       http://www.e-texteditor.com

Regex cheatsheet

  • 1.
    Regular Expressions -Quick Reference Guide Anchors Literal Characters ^ Character group contents start of line $ end of line Letters and digits match exactly axB70 x individual chars b word boundary Some special characters match exactly @-=% x-y character range B not at word boundary [:class:] posix char class A start of subject Escape other specials with backslash . $ [ G first match in subject [^:class:] negated class z end of subject Character Groups Z end of subject Almost any character (usually not newline) . Examples or before newline at end [a-zA-Z0-9_] Lists and ranges of characters [ ] Non-printing characters Any character except those listed [^ ] [[:alnum:]_] a alarm (BEL, hex 07) cx "control-x" e escape (hex 1B) Counts (add ? for non-greedy) Comments f formfeed (hex 0C) 0 or more ("perhaps some") * (?#comment) n newline (hex 0A) 0 or 1 ("perhaps a") ? r carriage return (hex OD) t 1 or more ("some") + Conditional subpatterns tab (hex 09) ddd octal code ddd Between "n" and "m" of {n,m} (?(condition)yes-pattern) xhh hex code hh Exactly "n", "n" or more {n}, {n,} (?(condition)yes|no-pattern) x{hhh..} hex code hhh.. Generic character types Alternation Recursive patterns d decimal digit Either/or | (?n) Numbered D not a decimal digit (?0) (?R) Entire regex s whitespace character S Lookahead and Lookbehind (?&name) Named not a whitespace char w "word" character Followed by (?= ) W "non-word" character NOT followed by (?! ) Replacements POSIX character classes Following (?<= ) $n reference capture alnum letters and digits NOT following (?<! ) alpha letters Case foldings ascii character codes 0-127 blank Grouping u upper case next char space or tab only cntrl control characters For capture and counts ( ) U upper case following digit decimal digits Non-capturing (?: ) l lower case next char graph printing chars -space L lower case following lower lower case letters Named captures (?<name> ) print printing chars +space Alternation E end case folding punct printing chars -alnum Back references space white space Conditional insertions upper Numbered n gn g{n} upper case letters (?n:insertion) word "word" characters Relative g{-n} xdigit hexadecimal digits Named k<name> (?n:insertion:otherwise) http://www.e-texteditor.com