This document provides a summary of common regular expression (regex) syntax including anchors, quantifiers, character classes, escape characters, modifiers, replacements, assertions and sample patterns. It explains regex tokens for matching the start/end of a string, words, lines, character ranges, repetition, groups, alternatives, lookarounds, comments and more. Examples are given for matching dates, images, colors, passwords, emails and HTML tags.
This document provides a summary of common regular expression (regex) syntax including anchors, quantifiers, character classes, escape characters, modifiers, replacements, assertions and sample patterns. It explains regex tokens for matching the start/end of a string, words, lines, character ranges, repetition, groups, alternatives, lookarounds, comments and more. Examples are given for matching dates, images, colors, passwords, emails and HTML tags.
This document provides a summary of common regular expression (regex) syntax including anchors, quantifiers, character classes, escape characters, modifiers, replacements, assertions and sample patterns. It explains regex tokens for matching the start/end of a string, words, lines, character ranges, repetition, groups, alternatives, lookarounds, comments and more. Examples are given for matching dates, images, colors, passwords, emails and HTML tags.
This document provides a summary of common regular expression (regex) syntax including anchors, quantifiers, character classes, escape characters, modifiers, replacements, assertions and sample patterns. It explains regex tokens for matching the start/end of a string, words, lines, character ranges, repetition, groups, alternatives, lookarounds, comments and more. Examples are given for matching dates, images, colors, passwords, emails and HTML tags.
^ Start of string * 0 or more . Any character except
\A Start of string + 1 or more new line (\n) $ End of string ? 0 or 1 (a|b) a or b \Z End of string {3} Exactly 3 (...) Group \b Word boundary {3,} 3 or more (?:...) Passive Group \B Not word boundary {3,5} 3, 4 or 5 [abc] Range (a or b or c) \< Start of word [^abc] Not a or b or c \> End of word [a-q] Letter between a and q Quantifier Modifiers [A-Q] Upper case letter "x" below represents a quantifier between A and Q Character Classes x? [0-7] Digit between 0 and 7 Ungreedy version of "x" \n nth group/subpattern \c Control character Note: Ranges are inclusive. \s White space Escape Character \S Not white space \d Digit \ Pattern Modifiers Escape Character \D Not digit \w g Global match Word \W Metacharacters (must be escaped) i Case-insensitive Not word \x m Multiple lines Hexadecimal digit ^ [ . s Treat string as single line \O Octal digit $ { * x Allow comments and ( \ + white space in pattern POSIX ) | ? e Evaluate replacement < > U Ungreedy pattern [:upper:] Upper case letters [:lower:] Lower case letters [:alpha:] All letters Special Characters String Replacement (Backreferences) [:alnum:] Digits and letters [:digit:] Digits \n New line $n nth non-passive group [:xdigit:] Hexadecimal digits \r Carriage return $2 "xyz" in /^(abc(xyz))$/ [:punct:] Punctuation \t Tab $1 "xyz" in /^(?:abc)(xyz)$/ [:blank:] Space and tab \v Vertical tab $` Before matched string [:space:] Blank characters \f Form feed $' After matched string [:cntrl:] Control characters \xxx Octal character xxx $+ Last matched string [:graph:] Printed characters \xhh Hex character hh $& Entire matched string [:print:] Printed characters and spaces Sample Patterns [:word:] Digits, letters and underscore Pattern Will Match ([A-Za-z0-9-]+) Letters, numbers and hyphens (\d{1,2}\/\d{1,2}\/\d{4}) Date (e.g. 21/3/2006) Assertions ([^\s]+(?=\.(jpg|gif|png))\.\2) jpg, gif or png image ?= Lookahead assertion (^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^50$) Any number from 1 to 50 inclusive ?! Negative lookahead (#?([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?) Valid hexadecimal colour code ?<= Lookbehind assertion ((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}) String with at least one upper case ?!= or ?<! Negative lookbehind letter, one lower case letter, and one ?> Once-only Subexpression digit (useful for passwords). ?() Condition [if then] (\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}) Email addresses ?()| Condition [if then else] (\<(/?[^\>]+)\>) HTML Tags ?# Comment Note: These patterns are intended for reference purposes and have not been extensively tested. Please use with caution and test thoroughly before use. Available free from www.ILoveJackDaniels.com