Skip to content

Instantly share code, notes, and snippets.

@stevenocchipinti
Last active February 1, 2018 17:51
Show Gist options
  • Save stevenocchipinti/57711d2d61333416976ed7d67b832949 to your computer and use it in GitHub Desktop.
Save stevenocchipinti/57711d2d61333416976ed7d67b832949 to your computer and use it in GitHub Desktop.
Regex

Regular Expressions

  • Simple characters

    • abc
  • Quantifiers

    • * 0 or more
    • + 1 or more
    • ? 0 or 1
    • {2} exactly 2
    • {2-4} 2, 3 or 4
    • {2,} 2 or more
  • More characters

    • . wildcard, any character except line break
    • \d a digit, \D not a digit
    • \w word character, \W not a word character
    • \s whitespace character, \S not a whitespace character
    • a|b logical OR
  • Character classes

    • [abc] character classes
    • [a-zA-Z] ranged character class
    • [^abc] negated character class
  • Anchors (zero length)

    • ^ start of line
    • $ end of line
    • \b word boundary
    • \a start of string
    • \z end of string (including trailing \n)
    • \Z end of string (excluding trailing \n)
  • Capture groups

    • (abc) standard capture group
    • (?:abc) non capturing group
    • \1, \2 captured groups
    • Ordered by the open brackets
  • Look arounds

    • a(?=b) positive lookahead, match an a that is followed by a b (but don't match the b)
    • a(?!b) negative lookahead, match an a that is NOT followed by a b (but don't match what follows the a)
    • (?<=a)b positive lookbehind, match a b that is proceeded by a a (but don't match the a)
    • (?<!a)b negative lookbehind, match a b that is NOT proceeded by a a (but don't match what proceeds the b)
  • Modifiers

    • Case-insensitivity (/i), global (/g), multiline (/m), etc.
    • There are more (ungreedy, line endings, etc.)
    • Often better to look at the tool/language as they vary a lot (/i, (?i), etc.)
  • Tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment