Regular expressions
Regular expressions (regex) are used in many situations. Web server administration and search algorithms are just a couple of the tasks where I’ve found myself looking up references on them. Rather than always searching for the latest search engine optimized page on the internet, I figure I should just keep it here…
| Syntax | Definition | Usage |
| . | Any character | /l.mb/ - Will match “lamb” or “limb” (or any other l_mb) |
| ? | Matches preceding character (or group) zero or one time | /Brand?/ - Matches both “Bran” and “Brand” |
| + | Matches preceding character (or group) one or more times | /Class A+/ - Matches “Class A”, “Class AA”, “Class AAA”, and so on |
| * | Matches preceding character (or group) any number of times | /abcd*/ - Matches “abc”, “abcd”, “abcdd”, “abcddd”, and so on |
| {n} | Matches preceding character (or group) n number of times | /Mag{2}et{2}e/ - Matches “Maggette” |
| {n,m} | Matches preceding character (or group) between n and m number of times inclusive | /Clip{1,3}ers/ - Matches “Clipers”, “Clippers”, and “Clipppers” |
| {n,} | Matches preceding character (or group) n or more times | /Clip{2,}ers/ - Matches “Clippers”, “Clipppers”, “Clippppers”, and so on |
| ^ | Beginning of the string/line | /^begin/ - Find strings/lines where the first characters are “begin” |
| $ | End of the string/line | /end$/ - Find strings/lines where the last characters are “end” |
| [abcd] | Any of the characters | /sa[dmw]/ - Matches “sad”, “sam”, and “saw” |
| [^abcd] | Not one of the characters | /sa[^dm]/ - Matches anything that contains “sa” followed by another character except for “sad” and “sam” |
| [a-d] | Any character in the range | /[a-d]/ - Matches a, b, c, or d. Can use numbers too like /[1-5]/ to match 1, 2, 3, 4, or 5 |
| (abcd) | Grouping | /(Elton)+/ - Matches one or more occurances of “Elton” |
| (a|b) | a or b | /(Chris|Shaun)/ - Matches either “Chris” or “Shaun” |
| !(a) | Not in this group | /!(Donald)/ - Doesn’t contain “Donald” |
| *?, +?, ??, {n}? | Least number of possible matches | /bo+?/ - Given the string, “boo”, would return “bo” /bo+/ - Would return “boo” in this case |
