Webmaster Resources Center

Welcome to the Bravenet Resource Center. Use these resources to help make your web pages more advanced and fun to use.

Articles & Tutorials

Introduction To Regular Expressions In PHP

by Bernard Peh


« Prev · 1 · 2 · 3 · Next »

Summary : In Linux and Unix, the syntax that is commonly used by many applications for specifying text patterns is known as regular expressions or in short form - regex.

In Linux and Unix, the syntax that is commonly used by many applications for specifying text patterns is known as regular expressions or in short form - regex. Regex is a very powerful technique to describe patterns and many programs use them to describe sequences of characters to be matched. Search programs such as 'grep' rely heavily on regex. Basically regex forms the core in the linux world. Many scripting languages such as perl, ruby, php...etc has build in regex functions as well. So you can see, learning regular expression is important because they are used alot in many places and probably more so in the future.

Regex can be scary at first but if you can get the basics, it is really not too hard to understand. In this article, we are going to look at how regex comes into the picture when writing php applications.

To do a quick summary so far, a regular expression is a sequence of literal characters, wildcards, modifiers and anchors.

Literal Characters

Literal characters are letters, digits and special characters that match only themselves. Examples are abc, 123, ~@ and so on (some characters are reserved though).

  • An inclusion range [m-n] matches one of any character included in the range from m to n.

Example '[a-z]' will match any alpha character that falls within the a to z range.

  • An exclusion range [^m-n] matches one of any character not included in the range from m to n. Example '[^0-9]' will match any non-digit character.
  • A period "." matches any character. It is also known as the wildcard. Example 'a.c' will match 'aec', 'acc', 'a@a' and so on.
  • The escape character '' enable interpretation of special characters. Example 'a.c' will match 'ac' only. Remember that '.' is a reserved character to represent a wildcard? Therefore to match a period, ie '.', we need to escape it like so '.'
  • The expression [:alnum:] will match all alpha-numeric characters. It is a shortcut to [A-Za-z0-9]. As you can see, it is not really a shortcut. The expression [:alnum:] might be easier to remember for some people.
  • The expression [:alpha:] will match all alpha characters. It is a shortcut to [A-Za-z].
  • The expression [:blank:] will match a space or tab.
  • The expression [:digit:] will match a numeric digit. It is a shortcut to [0-9].
  • The expression [:lower:] will match all lowercase letters. It is a shortcut to [a-z].
  • The expression [:upper:] will match all uppercase letters. It is a shortcut to [A-Z].
  • The expression [:punct:] will match all printable characters, excluding spaces and alphanumerics.
  • The expression [:space:] will match a whitespace character.

« Prev · 1 · 2 · 3 · Next »