I thought it would be useful if I created a quick regular expression reference for fellow developers to refer back to, as it is one of the finer points of programming that many developers don’t memorise very well. I use regular expressions pretty often but even so, sometimes a complex regex rule can stump me without referring back to something (usually things like lookahead rules – scary!)
Regular expressions are basically just patterns that we can use to match strings against. In PHP we can use the preg_match() to return true if the pasttern is matched and false if not, or preg_replace to replace portions of a string that match a regex pattern.
Here are the most used regular expressions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
. // matches any character apart from a newline ^ // indicates the start of a string $ // indicates the end of a string \ // escapes any special characters (eg . ^ $ etc) \d // matches a digit \w // matches a word character (e.g. A to Z, a to z, 0 to 9 or _) \s // matches whitespace \D // matches any character except a digit \W // matches any character except a word character \S // matches any character except whitespace [123] // matches 1, 2 or 3 [a-z] // matches any character a to z [0-9] // matches any digit 0 to 9 [^abc] // matches any character except a, b or c abc|xyz // matches abc or xyz ? // matches 0 or 1 of the preceding expression * // matches 0 or more of the preceding expression + // matches 1 or more of the preceding expression {6} // matches 6 of the preceding expression {5,} // matches 5 or more of the preceding expression {2,4} // matches between 2 and 4 of the preceding expression .*? // forces the expression find the fewest possible characters to match the rule (non-greedy) (expr) // captures the expression (?:expr) // does not not capture expression (?=expr) // matches if preceded by this expression (lookbehind) (?!expr) // matches if followed by this expression (lookahead) |
Here are some basic examples using the rules above that you can use:
1 2 3 4 5 6 7 8 |
// matches HTML links a captures the link and link text <a.*?href="(.*?)".*?>(.*?)</a> // matches a UK postcode [A-Z]{1,2}[0-9]{1,2}[A-Z]?\s?[0-9][A-Z]{2} // matches an email address (basic) ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$ |
Regular expressions are incredibly powerful as you can see, and these examples are very basic. Regular expression are essential for all sorts of application from web scraping to form validation. Hopefully you will find this resource something useful to come back to time and time again.