Skip to content

Instantly share code, notes, and snippets.

@magicznyleszek
Last active March 4, 2022 20:57
Show Gist options
  • Save magicznyleszek/53f8b04d558240764c25 to your computer and use it in GitHub Desktop.
Save magicznyleszek/53f8b04d558240764c25 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