Code Logo

Regex Extract

Published at24 Jul 2026
PHP String Handling Hard 0 views
Like0

Write a PHP function that extracts all matches of a regular expression pattern from a string using PHP's PCRE functions.

Call preg_match_all() with the pattern, subject string, and a matches variable passed by reference. This function executes a global regex match, finding all non-overlapping occurrences of the pattern.

The matches array follows a specific structure: $matches[0] contains all full pattern matches. If the pattern contains capture groups (parentheses), $matches[1] contains group 1 captures, $matches[2] contains group 2 captures, and so on.

If preg_match_all() returns a positive integer (the number of matches), return $matches[0]. If it returns 0 or false (no matches or error), return an empty array.

Edge cases include empty subject strings (return []), patterns with no matches in the subject (return []), and patterns with special regex characters that need escaping within the pattern string.

Time complexity depends on the regex pattern. Simple patterns run in O(n) time. Space complexity is O(m) where m is the number of matches found.

Regular expression matching is a powerful text processing technique. PHP's preg_match_all() provides global matching functionality, finding all non-overlapping occurrences of a pattern. The function returns the count of matches and populates the matches array by reference.

Example Input & Output

Example 1
Input
"test123abc456","/\d+/"
Output
["123","456"]
Example 2
Input
"Hello World","/[A-Z]/"
Output
["H","W"]
Example 3
Input
"","/\w+/"
Output
[]
Example 4
Input
"abc","/\d+/"
Output
[]
Example 5
Input
"a1b2c3","/[a-z]/"
Output
["a","b","c"]

Algorithm Flow

Recommendation Algorithm Flow for Regex Extract
Recommendation Algorithm Flow for Regex Extract

Solution Approach

Write a PHP function that extracts all matches of a regular expression pattern from a string using PHP's PCRE functions.

Call preg_match_all() with the pattern, subject string, and a matches variable passed by reference. This function executes a global regex match, finding all non-overlapping occurrences of the pattern.

The matches array follows a specific structure: $matches[0] contains all full pattern matches. If the pattern contains capture groups (parentheses), $matches[1] contains group 1 captures, $matches[2] contains group 2 captures, and so on.

If preg_match_all() returns a positive integer (the number of matches), return $matches[0]. If it returns 0 or false (no matches or error), return an empty array.

Edge cases include empty subject strings (return []), patterns with no matches in the subject (return []), and patterns with special regex characters that need escaping within the pattern string.

Time complexity depends on the regex pattern. Simple patterns run in O(n) time. Space complexity is O(m) where m is the number of matches found.

Best Answers

php - Approach 1
<?php
function regexExtract($subject, $pattern) {
    $m=[];$n=preg_match_all($pattern,$subject,$m);return $n?$m[0]:[];
}