preg_match_all

preg_match_all(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false

Searches a string for all matches of a regular expression (not just the first) and returns the match count.

C C23

C23
#include <stdio.h>
#include <regex.h>

int main(void) {
    regex_t re;
    // POSIX ERE has no \w/\d shorthand: use explicit character classes
    regcomp(&re, "([a-z]+)=([0-9]+)", REG_EXTENDED);
    const char *s = "a=1 b=2 c=3";
    regmatch_t m[3];
    int count = 0;
    const char *p = s;
    while (regexec(&re, p, 3, m, 0) == 0) { // loop: each call yields the next match
        printf("%.*s: key=%.*s value=%.*s\n",
               (int)(m[0].rm_eo - m[0].rm_so), p + m[0].rm_so,
               (int)(m[1].rm_eo - m[1].rm_so), p + m[1].rm_so,
               (int)(m[2].rm_eo - m[2].rm_so), p + m[2].rm_so);
        count++;
        p += m[0].rm_eo; // advance past this match
    }
    printf("count: %d\n", count);
    regfree(&re);
    return 0;
}
Output
a=1: key=a value=1
b=2: key=b value=2
c=3: key=c value=3
count: 3

C++ C++26

C++26
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string s = "a=1 b=2 c=3";
    std::regex re(R"(([a-z]+)=([0-9]+))"); // raw string avoids escaping

    // sregex_iterator walks every match; end-of-sequence iterator terminates
    auto end = std::sregex_iterator();
    int count = 0;
    for (auto it = std::sregex_iterator(s.begin(), s.end(), re); it != end; ++it) {
        std::cout << (*it)[0].str() << ": key=" << (*it)[1].str()
                  << " value=" << (*it)[2].str() << "\n";
        count++;
    }
    std::cout << "count: " << count << "\n";
    return 0;
}
Output
a=1: key=a value=1
b=2: key=b value=2
c=3: key=c value=3
count: 3

C# v11.0.100

v11.0.100
using System.Text.RegularExpressions;

var text = "a=1 b=2 c=3";
var matches = Regex.Matches(text, @"([a-z]+)=([0-9]+)"); // MatchCollection of every match

foreach (Match m in matches)
{
    Console.WriteLine($"{m.Value}: key={m.Groups[1].Value} value={m.Groups[2].Value}");
}
Console.WriteLine($"count: {matches.Count}");
Output
a=1: key=a value=1
b=2: key=b value=2
c=3: key=c value=3
count: 3

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`([a-z]+)=([0-9]+)`) // Go regex needs compiling first
    text := "a=1 b=2 c=3"

    // FindAllStringSubmatch: per match a slice [full, group1, group2]; -1 = all matches
    for _, m := range re.FindAllStringSubmatch(text, -1) {
        fmt.Printf("%s: key=%s value=%s\n", m[0], m[1], m[2])
    }
    fmt.Println("count:", len(re.FindAllStringSubmatch(text, -1)))
}
Output
a=1: key=a value=1
b=2: key=b value=2
c=3: key=c value=3
count: 3

Java v27.0.1

v27.0.1
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("([a-z]+)=([0-9]+)"); // compile first
        Matcher matcher = pattern.matcher("a=1 b=2 c=3");

        int count = 0;
        while (matcher.find()) { // find() advances through every match
            System.out.printf("%s: key=%s value=%s%n",
                    matcher.group(0), matcher.group(1), matcher.group(2));
            count++;
        }
        System.out.println("count: " + count);
    }
}
Output
a=1: key=a value=1
b=2: key=b value=2
c=3: key=c value=3
count: 3

JavaScript v26.13.1

v26.13.1
const text = "a=1 b=2 c=3";
const re = /([a-z]+)=([0-9]+)/g; // /g flag required: matchAll throws without it

// matchAll yields match objects with capture groups
for (const m of text.matchAll(re)) {
  console.log(`${m[0]}: key=${m[1]} value=${m[2]}`);
}
console.log("count:", [...text.matchAll(re)].length);
Output
a=1: key=a value=1
b=2: key=b value=2
c=3: key=c value=3
count: 3

PHP v8.6.0

v8.6.0
// default: $m[0] = all full matches, $m[1]/$m[2] = each capture group across matches
preg_match_all('/([a-z]+)=([0-9]+)/', 'a=1 b=2 c=3', $m);
var_export($m);
echo "\n";
// PREG_SET_ORDER: one sub-array per match [full, group1, group2]
preg_match_all('/([a-z]+)=([0-9]+)/', 'a=1 b=2 c=3', $m, PREG_SET_ORDER);
var_export($m);
echo "\n";
echo 'count: ', count($m), "\n";
Output
array (
  0 => 
  array (
    0 => 'a=1',
    1 => 'b=2',
    2 => 'c=3',
  ),
  1 => 
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
  2 => 
  array (
    0 => '1',
    1 => '2',
    2 => '3',
  ),
)
array (
  0 => 
  array (
    0 => 'a=1',
    1 => 'a',
    2 => '1',
  ),
  1 => 
  array (
    0 => 'b=2',
    1 => 'b',
    2 => '2',
  ),
  2 => 
  array (
    0 => 'c=3',
    1 => 'c',
    2 => '3',
  ),
)
count: 3

Python v3.15.2

v3.15.2
import re

text = "a=1 b=2 c=3"
pattern = r"([a-z]+)=([0-9]+)"

# findall returns tuples of the capture groups directly
print(re.findall(pattern, text))
# finditer streams match objects with .group(n)
for m in re.finditer(pattern, text):
    print(f"{m.group(0)}: key={m.group(1)} value={m.group(2)}")
print("count:", len(re.findall(pattern, text)))
Output
[('a', '1'), ('b', '2'), ('c', '3')]
a=1: key=a value=1
b=2: key=b value=2
c=3: key=c value=3
count: 3

Rust v1.99.1

v1.99.1
use regex::Regex; // regex crate

fn main() {
    let re = Regex::new(r"([a-z]+)=([0-9]+)").unwrap();
    let text = "a=1 b=2 c=3";

    // captures_iter streams a Captures per match
    let mut count = 0;
    for caps in re.captures_iter(text) {
        println!("{}: key={} value={}", &caps[0], &caps[1], &caps[2]);
        count += 1;
    }
    println!("count: {}", count);
}
Output
a=1: key=a value=1
b=2: key=b value=2
c=3: key=c value=3
count: 3

TypeScript v26.13.1

v26.13.1
const text: string = "a=1 b=2 c=3";
const re: RegExp = /([a-z]+)=([0-9]+)/g; // /g flag required for matchAll

for (const m of text.matchAll(re)) {
  console.log(`${m[0]}: key=${m[1]} value=${m[2]}`);
}
console.log("count:", [...text.matchAll(re)].length);
Output
a=1: key=a value=1
b=2: key=b value=2
c=3: key=c value=3
count: 3

Language differences

C: POSIX <regex.h> has no one-shot find-all API — loop with regexec and advance manually; POSIX ERE lacks \w/\d shorthands, use explicit character classes.

C++: std::sregex_iterator walks every match; range-for with std::sregex_token_iterator also works.

C#: Regex.Matches returns a lazy MatchCollection; its Count is the match total.

Go: the second argument -1 in FindAllStringSubmatch(s, -1) means all matches (pass n to take the first n).

Java: loop on find() which advances match by match; there is no array-returning find-all API.

JavaScript: matchAll (ES2020) returns an iterator and throws TypeError without the /g flag; older code used match + /g (full matches only, no capture groups).

PHP: unlike preg_match which stops at the first match, preg_match_all finds all and returns the total; $matches is by default grouped by capture group ($m[0] = full-match array, $m[1] = first-group array), and PREG_SET_ORDER flips it to grouped by match (one small array per match) — the latter suits per-match processing.

Python: re.findall returns a list of group tuples directly (fine for simple cases); re.finditer streams match objects (use when positions or laziness matter).

Rust: the regex crate's captures_iter streams matches; find_iter yields full matches only.

TypeScript: same as JS — matchAll requires the /g flag.