Content · Programming · PHP

Writing Regular Expressions with preg_*

Learn Writing Regular Expressions with preg_* in PHP with clear syntax, three code examples, important edge cases, common beginner mistakes, and related APIs.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When you need to construct and test PHP PCRE patterns, it helps to understand both the normal result and what changes with different inputs. This guide focuses on practical examples rather than repeating PHP background material.

Writing Regular Expressions with preg_* provides the behavior described below. Patterns have delimiters, optional modifiers, anchors, classes and quantifiers. Escape untrusted literal text with preg_quote().

What Is Writing Regular Expressions with preg_* in PHP?

Construct and test php pcre patterns. Patterns have delimiters, optional modifiers, anchors, classes and quantifiers. Escape untrusted literal text with preg_quote().

Its syntax is:

preg_match("/pattern/", $text)

Parameters and return value: Read the argument names and optional values in the signature above. Methods use an existing object, while stand-alone functions take direct arguments. The examples demonstrate the relevant arguments and the value returned or the object modified.

Important: Check for false separately from a successful non-match (0); input is byte-oriented unless you use /u.

Example 1: Basic Usage

This first example demonstrates how to construct and test PHP PCRE patterns with a small input.

<?php

$text = "Product-42";
var_dump(preg_match("/^Product-\d+$/", $text));

Output:

int(1)

The example uses the simplest useful form. Patterns have delimiters, optional modifiers, anchors, classes and quantifiers. Escape untrusted literal text with preg_quote(). The output may depend on the active ICU locale, PHP configuration, or input supplied to the function.

Example 2: Another Case

Here the input or option changes, making it easier to see where the behavior differs. Check for false separately from a successful non-match (0); input is byte-oriented unless you use /u.

<?php

$text = "one, two;three";
print_r(preg_split("/\s*[,;]\s*/", $text));

Output:

Array
(
    [0] => one
    [1] => two
    [2] => three
)

Compare the result to the first example. Use preg_match for testing, preg_match_all for all matches, and preg_replace for substitution.

Practical Example: Applying the Function

This standalone example shows how the same operation fits into a small application task.

<?php

$messages = ["WARN: slow", "INFO: ok", "WARN: retry"];
foreach ($messages as $message) {
    if (preg_match("/^WARN:\s*(.+)$/", $message, $matches) === 1) { echo $matches[1] . PHP_EOL; }
}

Output:

slow
retry

The example keeps the operation visible and gives you a starting point for using it in an application. Check for false separately from a successful non-match (0); input is byte-oriented unless you use /u.

Important Behavior and Edge Cases

  • Core behavior: Patterns have delimiters, optional modifiers, anchors, classes and quantifiers. Escape untrusted literal text with preg_quote().
  • Return value: Check the result separately from changes to passed-in arrays, iterators, or object state.
  • Data constraints: Check for false separately from a successful non-match (0); input is byte-oriented unless you use /u.
  • Environment: Run this example under a PHP version supporting the API; check results and exceptions before using them.

Comparison With Related Functions

Function or ApproachWhen to Use It
Writing Regular Expressions with preg_*Construct and test php pcre patterns.
Related PHP APIsUse preg_match for testing, preg_match_all for all matches, and preg_replace for substitution.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming the function works on every kind of inputFollow the declared argument types and check the input format before calling it.
Ignoring the behavior of the resultCheck for false separately from a successful non-match (0); input is byte-oriented unless you use /u.
Using the wrong related PHP APIUse preg_match for testing, preg_match_all for all matches, and preg_replace for substitution.

Key Takeaways

  • Writing Regular Expressions with preg_* helps you construct and test PHP PCRE patterns.
  • Patterns have delimiters, optional modifiers, anchors, classes and quantifiers. Escape untrusted literal text with preg_quote().
  • Check for false separately from a successful non-match (0); input is byte-oriented unless you use /u.
  • Use preg_match for testing, preg_match_all for all matches, and preg_replace for substitution.

Writing Regular Expressions with preg_* is useful when you need to construct and test PHP PCRE patterns. Start with the small examples, then adapt the practical example while respecting its input and environment requirements.

Share this resourceSend this page to someone who may find it useful.