Skip to content

Instantly share code, notes, and snippets.

@abehmiel
Forked from magicznyleszek/regex.md
Created December 11, 2017 21:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abehmiel/000849d09a0ec53a877ce5d40d2bbbf7 to your computer and use it in GitHub Desktop.
Save abehmiel/000849d09a0ec53a877ce5d40d2bbbf7 to your computer and use it in GitHub Desktop.
RegEx Cheatsheet

RegEx Cheatsheet

Contents:

  1. Special characters
  2. Quantifiers
  3. Special sequences
  4. Useful examples

Special characters

  • \ - escapes special characters
  • . - matches any character
  • ^ - matches start of the string (or line if MULTILINE)
  • $ - matches end of the string (or line if MULTILINE)
  • [5b-d] - matches any chars '5', 'b', 'c' or 'd'
  • [^a-c6] - matches any char except 'a', 'b', 'c' or '6'
  • R|S- matches either regex R or regex S
  • () - creates a capture group, and indicates precedence

Quantifiers

  • * - 0 or more
  • + - 1 or more
  • ? - 0 or 1
  • {m} - exactly 'm'
  • {m,n} - from m to n (m defaults to 0, n to infinity)
  • {m,n}? - from m to n, as few as possible

Special sequences

  • \A - Start of string
  • \b - Matches empty string at word boundary (between \w and \W)
  • \B - Matches empty string not at word boundary
  • \d - Digit
  • \D - Non-digit
  • \s - Whitespace: [ \t\n\r\f\v], more if LOCALE or UNICODE
  • \S - Non-whitespace
  • \w - Alphanumeric: [0-9a-zA-Z_], or is LOCALE dependant
  • \W - Non-alphanumeric
  • \Z - End of string

Useful examples

Find foo word:

\bfoo\b

Find a sequence of foo + some characters + bar:

\bfoo\b.*\bbarb\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment