preg_match
preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false
Searches a string for a match using a regular expression.
C C23
C23
#include <regex.h>
#include <stdio.h>
int main(void) {
regex_t re;
regcomp(&re, "[0-9]+", REG_EXTENDED); // POSIX regex (not C stdlib, but shipped with POSIX systems)
const char *s = "abc123def";
regmatch_t m[1];
if (regexec(&re, s, 1, m, 0) == 0) { // match succeeded
printf("%.*s\n", (int)(m[0].rm_eo - m[0].rm_so), s + m[0].rm_so);
}
regfree(&re); // free the regex object
// capture groups: pmatch[1..n] hold group offsets, like $m[1..n]
regex_t re2;
regcomp(&re2, "([0-9]+)-([0-9]+)-([0-9]+)", REG_EXTENDED);
regmatch_t g[4];
const char *date = "2026-11-19";
if (regexec(&re2, date, 4, g, 0) == 0) {
printf("year=%.*s month=%.*s day=%.*s\n",
(int)(g[1].rm_eo - g[1].rm_so), date + g[1].rm_so,
(int)(g[2].rm_eo - g[2].rm_so), date + g[2].rm_so,
(int)(g[3].rm_eo - g[3].rm_so), date + g[3].rm_so);
}
regfree(&re2);
return 0;
}
Output
123 year=2026 month=11 day=19
C++ C++26
C++26
#include <iostream>
#include <regex>
#include <string>
int main() {
std::smatch m; // match result
std::string s = "abc123def";
std::regex_search(s, m, std::regex(R"(\d+)")); // raw string avoids escaping
std::cout << m[0] << "\n";
// capture groups: m[1..n] hold the groups, like $m[1..n]
std::string date = "2026-11-19";
std::smatch d;
std::regex_search(date, d, std::regex(R"((\d+)-(\d+)-(\d+))"));
std::cout << "year=" << d[1] << " month=" << d[2] << " day=" << d[3] << "\n";
return 0;
}
Output
123 year=2026 month=11 day=19
C# v11.0.100
v11.0.100
var m = System.Text.RegularExpressions.Regex.Match("abc123def", @"\d+");
Console.WriteLine(m.Value);
// capture groups: Groups[n] gives each group, like $m[1..n]
var dm = System.Text.RegularExpressions.Regex.Match("2026-11-19", @"(\d+)-(\d+)-(\d+)");
Console.WriteLine($"year={dm.Groups[1].Value} month={dm.Groups[2].Value} day={dm.Groups[3].Value}");
Output
123 year=2026 month=11 day=19
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\d+`)
fmt.Println(re.FindString("abc123def"))
// capture groups: FindStringSubmatch returns [full, g1, g2, ...]
dm := regexp.MustCompile(`(\d+)-(\d+)-(\d+)`).FindStringSubmatch("2026-11-19")
fmt.Printf("year=%s month=%s day=%s\n", dm[1], dm[2], dm[3])
}
Output
123 year=2026 month=11 day=19
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) {
Matcher m = Pattern.compile("\\d+").matcher("abc123def");
if (m.find()) {
System.out.println(m.group());
}
// capture groups: group(n) reads each group, like $m[1..n]
Matcher dm = Pattern.compile("(\\d+)-(\\d+)-(\\d+)").matcher("2026-11-19");
if (dm.find()) {
System.out.printf("year=%s month=%s day=%s%n", dm.group(1), dm.group(2), dm.group(3));
}
}
}
Output
123 year=2026 month=11 day=19
JavaScript v26.13.1
v26.13.1
const m = "abc123def".match(/\d+/);
console.log(m[0]);
// capture groups: m[1..n] hold the groups, like $m[1..n]
const d = "2026-11-19".match(/(\d+)-(\d+)-(\d+)/);
console.log(`year=${d[1]} month=${d[2]} day=${d[3]}`)
Output
123 year=2026 month=11 day=19
PHP v8.6.0
v8.6.0
preg_match('/\d+/', "abc123def", $m);
echo $m[0], "\n";
// capture groups land in $m[1..n]: parse a date into parts
preg_match('/(\d+)-(\d+)-(\d+)/', '2026-11-19', $d);
echo "year={$d[1]} month={$d[2]} day={$d[3]}";
Output
123 year=2026 month=11 day=19
Python v3.15.2
v3.15.2
import re
m = re.search(r"\d+", "abc123def")
print(m.group(0))
dm = re.search(r"(\d+)-(\d+)-(\d+)", "2026-11-19")
print(f"year={dm.group(1)} month={dm.group(2)} day={dm.group(3)}") # capture groups
Output
123 year=2026 month=11 day=19
Rust v1.99.1
v1.99.1
fn main() {
// regex crate: Regex::new compiles the pattern (returns Err on failure), find takes the first match
let re = regex::Regex::new(r"\d+").unwrap();
let m = re.find("abc123def").unwrap();
println!("{}", m.as_str()); // equivalent to $m[0] from preg_match
// capture groups: captures() gives group access, like $m[1..n]
let caps = regex::Regex::new(r"(\d+)-(\d+)-(\d+)").unwrap()
.captures("2026-11-19").unwrap();
println!("year={} month={} day={}", &caps[1], &caps[2], &caps[3]);
}
Output
123 year=2026 month=11 day=19
TypeScript v26.13.1
v26.13.1
const m = "abc123def".match(/\d+/);
console.log(m![0]); // TS: match may return null, so it needs a non-null assertion (a match is guaranteed here)
const d = "2026-11-19".match(/(\d+)-(\d+)-(\d+)/);
console.log(`year=${d![1]} month=${d![2]} day=${d![3]}`); // capture groups
Output
123 year=2026 month=11 day=19
Language differences
C: uses POSIX <regex.h> (not ISO C, but shipped with POSIX systems).
C++: uses <regex> (ECMAScript grammar by default); raw strings R"(...)" avoid escaping.
C#: matches with Regex.Match (System.Text.RegularExpressions); a miss yields an empty Match.
Go: requires compiling a regex object first.
Java: requires compiling a regex object first.
JavaScript: match returns an array.
PHP: preg_match returns 0/1 and writes captures into the by-reference argument $m.
Python: re.search returns a match object read with .group().
Rust: regexes use the regex crate — Regex::new compiles the pattern and find returns the first match (captures gives capture groups, like PHP $m).