Quick regular expression reference

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:

Here are some basic examples using the rules above that you can use:

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.

Leave a Reply