CoryKelly
6/4/2018 - 7:34 PM

RegExCheatSheet.txt

RegEx
/regex/g  global - matches all

 .  is a wildcard
 \  escape 
" "  space is a character
\t  Tabs
\r. \n, \r\n  new line
\u  unicode

CHARACTER SET
[] character set, /[aeiou]/ match one vowel, order does not matter

CHARACTER RANGES
[0-9], [A-Za-z], [a-ek-ou-y], [50-99] matches [5] or [0-9] or [9] which can be reduced to [0-9]

N5R 1C1 = [A-Z0-9][A-Z0-9][A-Z0-9] [A-Z0-9][A-Z0-9][A-Z0-9]

NEGATIVE CHARACTER SET
^  Negative character
/[^aeiou]/  matches any one consonant (non-vowel)

SHORTHAND CHARACTERS
\d  Digit           [0-9]
\w  Word            [a-zA-z0-9_] , includes underscore not hyphen
\s  Whitespace      [ \t\r\n]
\D  Not digit       [^0-9]
\W  Not word        [^a-zA-Z0-9]
\S  Not whitespace  [^ \t\r\n]

/[\w\-]/ matches a word character or hyphen

REPETITION METACHARACTER
*  matches preceding character zero or more times  item >= 0
+  matches preceding character one or more times   item >= 1
?  matches preceding character zero or one times   item == 0 || item == 1

/apples*/ matches 'apple', 'apples', 'applesssss'.
/apples+/ matches 'apples' and 'applesssss'.
/apples?/ matches 'apple' and 'apples'.

QUANTIFIED REPETITION
{min,max}

\d{4,8}  matches numbers with 4 to 8 digits  
\d{4}    matches numbers with exactly 4 digits
\d{4,}   matches numbers with 4 or more digits

GREEDY EXPRESSIONS
/\d+\w+\d+/  matches  01_FY_07_report_99  from  01_FY_07_report_99
/".+" , ".+"/  matches  "Milton", "Waddams" , "Initech, Inc."  from  "Milton", "Waddams" , "Initech, Inc."  

LAZY EXPRESSIONS
?  makes preceding quantifier lazy

*? , +? , {min,max}? , ??

/\d+\w+?\d+/  matches  01_FY_07  from  01_FY_07_report_99
/".+?" , ".+"/  matches  "Milton", "Waddams"  from   "Milton", "Waddams" , "Initech, Inc."

EFFICIENCY
Define the quantity of repeated expressions:
/.+/ is faster than /.*/
/.{5}/ and /.{3,7}/ are even faster

Narrow the scope of the repeated expression
/.+/ can become /[A-Za-z]+/

Provide clearer starting and ending points
/<.+>/ can become /<[^>]+>/

GROUPING METACHARACTERS
( )  Group portions of the expression

/(abc)+/  matches "abc" and "abcabcabc"
/(in)?dependent/ matches "independent" and "dependent"
/runs(s)?/ is the same as /runs?/

ALERTNATION METACHARACTER
|  means  OR, match expression of the left or match expression on the right
Can be daisy chained.

/apple|orange/  matches  "apple" and "orange"
/abc|def|ghi/jkl  matches "abc", "def", "ghi", and "jkl".
/apple(juice|sauce)/ is not the same as /applejuice|sauce/
/w(ei|ie)rd/  matches "weird" or "wierd".