PHP regular expressions cheat sheet

Updated: January 10, 2024 By: Guest Contributor Post a comment

Introduction

Regular expressions are a powerful tool for pattern matching and text manipulation in PHP. This cheat sheet provides a handy reference to common RegEx functions and patterns.

Basic Patterns

Let’s start with some basic patterns and how you can use them in PHP.

Matching Any Character

$pattern = '/./';
if (preg_match($pattern, 'a')) {
    echo 'Match found.';
}

Matching Specific Characters

$pattern = '/[aeiou]/';
if (preg_match($pattern, 'sky')) {
    echo 'Vowel found.';
}

Matching Character Ranges

$pattern = '/[a-z]/';
if (preg_match($pattern, 'PHP 8')) {
    echo 'Letter found.';
}

Escaping Special Characters

$pattern = '/\d/'; // Matches any digit
if (preg_match($pattern, 'Number 5')) {
    echo 'Digit found.';
}

Quantifiers

Quantifiers specify the number of times a character, a group, or a character class must be present in the input for a match to be found.

Using the Star Quantifier

$pattern = '/0*/'; // Matches zero or more '0'
if (preg_match($pattern, '00011')) {
    echo 'Match found.';
}

Using the Plus Quantifier

$pattern = '/\d+/'; // Matches one or more digits
if (preg_match($pattern, 'Page 123')) {
    echo 'Match found.';
}

Using the Question Mark Quantifier

$pattern = '/colou?r/'; // Matches 'color' or 'colour'
if (preg_match($pattern, 'favorite color')) {
    echo 'Match found.';
}

Positional Assertions

Positional assertions determine a match based on the position of the pattern in the string.

Using Caret for Start of String

$pattern = '/^PHP/';
if (preg_match($pattern, 'PHP is fun')) {
    echo 'String starts with PHP.';
}

Using Dollar for End of String

$pattern = '/fun$/';
if (preg_match($pattern, 'PHP is fun')) {
    echo 'String ends with fun.';
}

Grouping and Capturing

Groups and captures allow for the combination of different patterns and extraction of matched sub-patterns.

Using Parentheses for Grouping

$pattern = '/(PHP|Java) is fun/';
if (preg_match($pattern, 'PHP is fun')) {
    echo 'Match found with PHP or Java.';
}

Backreferences

$pattern = '/(\d+) \1/';
if (preg_match($pattern, '42 42')) {
    echo 'Match found with repeated number.';
}

Lookahead and Lookbehind Assertions

Lookahead and lookbehind assertions check for a match without including it in the result.

Positive Lookahead

$pattern = '/\d(?=px)/';
if (preg_match($pattern, 'The width is 100px.')) {
    echo 'Value found before px.';
}

Negative Lookahead

$pattern = '/\d(?!px)/';
if (preg_match($pattern, 'The value is 100pt.')) {
    echo 'Number not followed by px found.';
}

Modifiers

Modifiers change how the regex pattern is interpreted. They can be used for case insensitive matching, multiline searches, and more.

Case Insensitive Match

$pattern = '/php/i';
if (preg_match($pattern, 'PHP is popular.')) {
    echo 'Case insensitive match found.';
}

Multiline Match

$pattern = '/^script/m';
if (preg_match($pattern, "Multiline\nscript")) {
    echo 'Match at the start of a line found.';
}

Advanced Techniques

As you get more comfortable with regular expressions, you may need advanced techniques like conditional patterns and non-capturing groups.

Non-capturing Groups

$pattern = '/(?:foo)/'; // Matches 'foo' but does not capture it
if (preg_match($pattern, 'foobar')) {
    echo 'Match found.';
}

Named Captures

$pattern = '/(?<name>\w+)/';
if (preg_match($pattern, 'John', $matches)) {
    echo 'Match found for ' . $matches['name'] . '.';
}

Recursive Patterns

$pattern = '/(\w+) \g<1\>/';
if (preg_match($pattern, 'recurse recurse')) {
    echo 'Match found with recursive pattern.';
}

Conclusion

This cheat sheet is a quick reference to the regular expression syntax and functions used in PHP. Whether you’re validating input, searching text, or transforming data strings, mastering PHP regex will significantly enhance your programming skills.