Skip to main content

What is a Regular Expression?

A regular expression describes what you are looking for in a text. You write a pattern made of characters, and the search uses that pattern to find matches.

Patterns can be as small as one character or far more elaborate.

Once you have a pattern, you can use it for any kind of text searching or replacing.


Syntax

PHP writes a regular expression as a string with up to three parts: delimiters, the pattern itself and, if you need them, modifiers.

Regular Expression Syntax

Here the slashes are the delimiters, PromptCookbook is the pattern and i is a modifier:

index.php
$exp = "/PromptCookbook/i";

Regular Expression Functions

Several PHP functions work with regular expressions. These three are the ones you will use most:

  • preg_match() - Gives 1 if the string contains the pattern, otherwise 0
  • preg_match_all() - Counts how many times the pattern occurs in the string; the count can be 0
  • preg_replace() - Gives back a new string with the matched parts swapped for another string

Using preg_match()

To find out whether a string contains a match for a pattern, call preg_match().

preg_match() Example

Search a string for "PromptCookbook" while ignoring case. The result is 1 because a match exists:

Prompt
Write a PHP script that uses preg_match() to do a case-insensitive search for "PromptCookbook" in the string "Visit PromptCookbook" and outputs whether a match was found.
Output: PHP script

Using preg_match_all()

When you need the number of matches for a pattern in a string, use preg_match_all().

preg_match_all() Example

Count how many times "ain" appears in a string, ignoring case:

Prompt
Write a PHP script that uses preg_match_all() to do a case-insensitive count of the number of occurrences of "ain" in the string "The rain in Spain falls mainly on the plain" and outputs the count.
Output: PHP script

Using preg_replace()

With preg_replace(), every match of the pattern in a string gets swapped for another string.

preg_replace() Example

Replace Microsoft with PromptCookbook in a string, using a pattern that ignores case:

Prompt
Write a PHP script that uses preg_replace() to do a case-insensitive replacement of "Microsoft" with "PromptCookbook" in the string "Visit Microsoft!" and outputs the result.
Output: PHP script

Regular Expression Modifiers

Adding a modifier changes the way a search behaves.

  • i - Ignores the difference between uppercase and lowercase
  • m - Turns on multiline mode, so patterns for the start or end of a string match at the start or end of every line
  • u - Makes patterns encoded in UTF-8 match correctly

Regular Expression Patterns

Square brackets let you match one character out of a set or range:

  • [abc] - One character, chosen from those listed in the brackets
  • [^abc] - One character that is not listed in the brackets
  • [a-z] - One lowercase letter
  • [A-z] - One letter, uppercase or lowercase; this range also covers the special characters that sit between Z and a
  • [A-Z] - One uppercase letter
  • [123] - One digit, chosen from those listed in the brackets
  • [0-5] - One digit from 0 through 5
  • [0-9] - One digit from 0 through 9

Metacharacters

Some characters are not taken literally in a pattern. These metacharacters each have their own job:

  • | - Separates alternatives, and any one of them can match, as in cat|dog|fish
  • . - Exactly one character of any kind
  • ^ - Ties the match to the start of the string, as in ^Hello
  • $ - Ties the match to the end of the string, as in World$
  • \d - A digit
  • \s - A whitespace character
  • \b - A word edge: \bWORD matches at the start of a word, and WORD\b matches at the end of one
  • \x{xxxx} - The Unicode character whose hexadecimal number is xxxx (needs the u modifier)

Quantifiers

A quantifier says how many times something may occur:

  • n+ - A string with one or more n's
  • n* - A string where n appears any number of times, including none
  • n? - A string where n appears once or not at all
  • n{x} - A string with a run of exactly X n's
  • n{x,y} - A string with a run of X to Y n's
  • n{x,} - A string with a run of X or more n's

Grouping

Wrap part of a pattern in parentheses () and a quantifier will apply to that whole part. Parentheses also let you pick out parts of the pattern to use as a match.

Grouping Example

Find the word "banana" by searching for ba plus two copies of na:

Prompt
Write a PHP script that uses preg_match() with grouping to search for "banana" by looking for "ba" followed by two instances of "na" in the string "I like bananas" and outputs whether a match was found.
Output: PHP script