Regular Expressions (Regex or Regexp)

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.


  • 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:

Regex Resources

There are many good Regex tutorials and resources, of which I will list some below and add more in time.

And here are a couple of useful references for using Regex in specific applications.

Like this content? Why not share it?
Share on FacebookTweet about this on TwitterShare on LinkedInBuffer this pagePin on PinterestShare on Redditshare on TumblrShare on StumbleUpon
There Are No Comments
Click to Add the First »