facebook google plus twitter
Webucator's Free PHP Tutorial

Lesson: Regular Expressions

Welcome to our free PHP tutorial. This tutorial is based on Webucator's Introduction to PHP Training course.

Regular expressions are used to do sophisticated pattern matching. PHP supports two types of regular expressions: POSIX and Perl. The Perl style is more powerful and much more common, so we'll cover these in this class.

Lesson Goals

  • Understand how regular expressions work.
  • Use regular expressions for advanced form validation.

What is a Regular Expression

A regular expression is a pattern that specifies a list of characters. In this section, we will look at how those characters are specified. As we go through this section, we'll test some regular expression in our browser using our regular expression tester at RegExp/Demos/Tester.php.

Regular Expression Tester

We have created a simple PHP-based regular expression tester. You will find it in your class files at RegExp/Demos/Tester.php. Open it, and you will see a form like this: RegEx Tester

To use the regular expression tester, just put a regular expression starting with / and ending with / as the Pattern and any string as the Text to Search. Then click submit to see if the string matches the pattern.

Start and End ( ^ $ )

A caret (^) at the beginning of a regular expression indicates that the string being searched must start with this pattern.

  • The pattern ^foo can be found in "food", but not in "barfood".

A dollar sign ($) at the end of a regular expression indicates that the string being searched must end with this pattern.

  • The pattern foo$ can be found in "curfoo", but not in "food".

Number of Occurrences ( ? + * {} )

The following symbols affect the number of occurrences of the preceding character: ?, +, *, and {}.

Or characters if parentheses are used to create subpatterns.

A questionmark (?) indicates that the preceding character should appear zero or one times in the pattern.

  • The pattern foo? can be found in "food" and "fod", but not "faod".

A plus sign (+) indicates that the preceding character should appear one or more times in the pattern.

  • The pattern fo+ can be found in "fod", "food" and "foood", but not "fd".

A asterisk (*) indicates that the preceding character should appear zero or more times in the pattern.

  • The pattern fo*d can be found in "fd", "fod" and "food".

Curly brackets with one parameter ( {n} ) indicate that the preceding character should appear exactly n times in the pattern.

  • The pattern fo{3}d can be found in "foood" , but not "food" or "fooood".

Curly brackets with two parameters ( {n1,n2} ) indicate that the preceding character should appear between n1 and n2 times in the pattern.

  • The pattern fo{2,4}d can be found in "food","foood" and "fooood", but not "fod" or "foooood".

Curly brackets with one parameter and an empty second paramenter ( {n,} ) indicate that the preceding character should appear at least n times in the pattern.

  • The pattern fo{2,}d can be found in "food" and "foooood", but not "fod".

Common Characters ( . \d \D \w \W \s \S )

A period ( . ) represents any character except a newline.

  • The pattern fo.d can be found in "food", "foad", "fo9d", and "fo*d".

Backslash-d ( \d ) represents any digit. It is the equivalent of [0-9].

  • The pattern fo\dd can be found in "fo1d", "fo4d" and "fo0d", but not in "food" or "fodd".

Backslash-D ( \D ) represents any character except a digit. It is the equivalent of [^0-9].

  • The pattern fo\Dd can be found in "food" and "foad", but not in "fo4d".

Backslash-w ( \w ) represents any word character (letters, digits, and the underscore (_) ).

  • The pattern fo\wd can be found in "food", "fo_d" and "fo4d", but not in "fo*d".

Backslash-W ( \W ) represents any character except a word character.

  • The pattern fo\Wd can be found in "fo*d", "fo@d" and "fo.d", but not in "food".

Backslash-s ( \s) represents any whitespace character (e.g, space, tab, newline, etc.).

  • The pattern fo\sd can be found in "fo d", but not in "food".

Backslash-S ( \S ) represents any character except a whitespace character.

  • The pattern fo\Sd can be found in "fo*d", "food" and "fo4d", but not in "fo d".

Grouping ( [] )

Square brackets ( [] ) are used to group options. This creates what is referred to as a "character class".

  • The pattern f[aeiou]d can be found in "fad" and "fed", but not in "food", "faed" or "fd".
  • The pattern f[aeiou]{2}d can be found in "faed" and "feod", but not in "fod", "fed" or "fd".

Negation ( ^ )

When used after the opening square bracket of a character class, the caret ( ^ ) is used for negation.

  • The pattern f[^aeiou]d can be found in "fqd" and "f4d", but not in "fad" or "fed".

Subpatterns ( () )

Parentheses ( () ) are used to capture subpatterns.

  • The pattern f(oo)?d can be found in "food" and "fd", but not in "fod".

Alternatives ( | )

The pipe ( | ) is used to create optional patterns.

  • The pattern foo$|^bar can be found in "foo" and "bar", but not "foobar".

Escape Character ( \ )

The backslash ( \ ) is used to escape special characters.

  • The pattern fo\.d can be found in "fo.d", but not in "food" or "fo4d".

preg_match() and preg_replace()

preg_match()

The syntax for preg_match() is as follows.

preg_match(pattern, text_to_search);

preg_match() returns 1 if pattern is found in text_to_search and 0 if it is not.

preg_replace()

The syntax for preg_replace() is as follows.

preg_replace(pattern, replacement, text_to_search);

preg_replace() replaces all instances of pattern in text_to_search with replacement.

Form Validation Functions with Regular Expressions

Regular expressions can be used to write sophisticated form validation functions. For example, earlier in the course, we wrote a checkEmail() function that looked like this:

function checkEmail($email)
{
	$email = trim($email);
	if (!checkLength($email,6))
	{
		return false;
	}
	elseif (!strpos($email,'@'))
	{
		return false;
	}
	elseif (!strpos($email,'.'))
	{
		return false;
	}
	elseif (strrpos($email,'.') < strpos($email,'@'))
	{
		return false;
	}
	return true;
}

We can use a regular expression to make this function both simpler and more powerful:

function checkEmail($email)
{
	$emailPattern = '/^(\w+\.)*\w+@(\w+\.)+[A-Za-z]+$/';
	return preg_match($emailPattern,$email));
}

A nice thing about this is that we can use virtually the same function to do client-side validation with JavaScript:

function checkEmail(EMAIL)
{
	var reEmail = /^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/;
	return reEmail.test(EMAIL));
}

So, by using regular expressions in this way, you make it easy to create a similar function library on the client side.