Regular expressions (abbreviated as regex or regexp) are used by many text editors, utilities, and programming languages (in our case, PHP) to search and manipulate text based on patterns, most notably the presence of particular characters, words, or patterns of characters. Regular expressions are somewhat archaic in appearance and can be very intimidating to those not experienced in their use, but once you see how useful they are you will no doubt find the learning curve worth the effort.
Below is a list a, intended only as a very quick reference, of some of the most common regular expressions you might use.
- ^ – Match only if the Following is at the Beginning of the line
- $ – Match only if the Previous is at the End of the line
- ? – Match the Previous 0 or 1 times (makes it optional)
- . – Match any single character
- * – Match the Previous 0 or more times
- + – Match the Previous 1 or more times
- (x) – Matches x and remembers the match to be called using $1
- (?:x) – Matches x but does Not remember the match
- x(?=y) – Matches x Only if it is followed by y
- x(?!y) – Matches x only if it is NOT followed by y
- x|y – Matches x or y
- [xyz] – Matches any single character enclosed in the Brackets.
Note: This matches X, or y, or z; it doesn’t match the string “xyz”. - [^xyz] – Matches any single character except for those listed in the brackets.
- \ – “Escape” special characters. Thus \? literally matches ? not the Syntax Rule of “previous is option”.
- \d \w \s – Matches a Digit (number), Alphabetic Character (letter) or White Space (space bar) respectively.
- \D \W \S – Matches everything But a Digit, Character, or White Space respectively.
Examples:
- a+ matches "a", "aaaa", "aaaaaaaaaaaa", but not "bbb"
- [ab]+ matches, "a", "b", or any length combination of the two
- .s?html? matches ".htm", ".shtm", ".html" or ".shtml"
- (.+)/1999/(.+) matches "subject/1999/psy1011/", and also stores "subject" in $1 and "psy1011/" in $2.
In PHP, the specific built-in functions you will use to implement regular expressions include:
- preg_grep — Return array entries that match the pattern
- preg_last_error — Returns the error code of the last PCRE regex execution
- preg_match_all — Perform a global regular expression match
- preg_match — Perform a regular expression match
- preg_quote — Quote regular expression characters
- preg_replace_callback — Perform a regular expression search and replace using a callback
- preg_replace — Perform a regular expression search and replace
- preg_split — Split string by a regular expression
Regex Resources
There are many good Regex tutorials and resources, of which I will list some below and add more in time.
- RegExr is a free online regular expression tester, lets you hone your expression language and terms down, giving you a box to put testing text in and highlighting the words that match your query.
- Regular Expression Tutorial: Learn How to Use and Get The Most out of Regular Expressions
- Sample Regular Expressions
- Regular Expressions Cheat Sheet
- Regular Expressions (RegEx) – Quick Reference
- Regular Expression Examples
And here are a couple of useful references for using Regex in specific applications.
- Notepad++: A guide to using regular expressions and extended search mode
- Dave Child’s mod_rewrite cheat sheet
Click to Add the First »