######################## ### RegEX CheatSheet ### ######################## Pattern Modifiers: g Global match i Case-insensitive m Multiple lines s Treat string as single line x Allow comments and white space in pattern e Evaluate replacement U Ungreedy pattern Characters: \d Most engines: one digit: from 0 to 9 \w Most engines: "word character": ASCII letter, digit or underscore \s Most engines: "whitespace character": space, tab, newline, carriage return, vertical tab \D One character that is not a digit as defined by your engine's \d \W One character that is not a word character as defined by your engine's \w \S One character that is not a whitespace character as defined by your engine's \s More Characters: . Any character except line break . Any character except line break \. A period (special character: needs to be escaped by a \) \ Escapes a special character \ Escapes a special character More White-Space: \t Tab \r Carriage return character \n Line feed character \r\n Line separator on Windows \N Perl, PCRE (C, PHP, R...): one character that is not a line break \h Perl, PCRE (C, PHP, R...), Java: one horizontal whitespace character: tab or Unicode space separator \H One character that is not a horizontal whitespace \v .NET, JavaScript, Python, Ruby: vertical tab \v Perl, PCRE (C, PHP, R...), Java: one vertical whitespace character: line feed, carriage return, vertical tab, form feed, paragraph or line separator \V Perl, PCRE (C, PHP, R...), Java: any character that is not a vertical whitespace \R Perl, PCRE (C, PHP, R...), Java: one line break (carriage return + line feed pair, and all the characters matched by \v) Quantifiers: + One or more {3} Exactly three times {2,4} Two to four times {3,} Three or more times * Zero or more times ? Once or none More Quantifiers: + The + (one or more) is "greedy" ? Makes quantifiers "lazy" * The * (zero or more) is "greedy" ? Makes quantifiers "lazy" {2,4} Two to four times, "greedy" ? Makes quantifiers "lazy" Logic: | Alternation / OR operand ( ... ) Capturing group \1 Contents of Group 1 \2 Contents of Group 2 (?: ... ) Non-capturing group Anchors and Boundaries ^ Start of string or start of linedepending on multiline mode. (But when [^inside brackets], it means "not") $ End of string or end of linedepending on multiline mode. Many engine-dependent subtleties. \A Beginning of string (all major engines except JS) \z Very end of the string (Not available in Python and JS) \Z End of string or (except Python) before final line break (Not available in JS) \G Beginning of String or End of Previous Match (.NET, Java, PCRE (C, PHP, R...), Perl, Ruby) \b Word boundary (Most engines: position where one side only is an ASCII letter, digit or underscore) \b Word boundary (.NET, Java, Python 3, Ruby: position where one side only is a Unicode letter, digit or underscore) \B Not a word boundary Character Classes: - Range indicator [ ... ] One of the characters in the brackets [x-y] One of the characters in the range from x to y [^x] One character that is not x [^x-y] One of the characters not in the range from x to y [\d\D] One character that is a digit or a non-digit [\x41] Matches the character at hexadecimal position 41 in the ASCII table, i.e. A POSIX Classes: [:alpha:] PCRE (C, PHP, R...): ASCII letters A-Z and a-z [:alpha:] Ruby 2: Unicode letter or ideogram [:alnum:] PCRE (C, PHP, R...): ASCII digits and letters A-Z and a-z [:alnum:] Ruby 2: Unicode digit, letter or ideogram [:punct:] PCRE (C, PHP, R...): ASCII punctuation mark [:punct:] Ruby: Unicode punctuation mark Lookarounds: (?=...) Positive lookahead (?<=...) Positive lookbehind (?!...) Negative lookahead (?