preg_grep
preg_grep(string $pattern, array $array, int $flags = 0): array|false
Returns the array elements that match a pattern (original keys preserved).
C C23
#include <regex.h>
#include <stdio.h>
int main(void) {
const char *words[] = {"red", "green", "blue"};
regex_t re;
regcomp(&re, "^(red|green)$", REG_EXTENDED); // compile the pattern once
int first = 1;
for (int i = 0; i < 3; i++) {
if (regexec(&re, words[i], 0, NULL, 0) == 0) { // keep the matching elements
if (!first) printf(",");
printf("%s", words[i]);
first = 0;
}
}
regfree(&re);
return 0;
}
red,green
C++ C++26
#include <iostream>
#include <regex>
#include <string>
#include <vector>
int main() {
std::vector<std::string> words{"red", "green", "blue"};
const std::regex re("^(red|green)$");
bool first = true;
for (const auto &w : words) {
if (std::regex_match(w, re)) { // regex_match requires a full-string match
if (!first) std::cout << ",";
std::cout << w;
first = false;
}
}
return 0;
}
red,green
C# v11.0.100
var words = new[] { "red", "green", "blue" };
var re = new System.Text.RegularExpressions.Regex("^(red|green)$");
var matched = words.Where(w => re.IsMatch(w)); // LINQ filter keeps the matches
Console.WriteLine(string.Join(",", matched));
red,green
Go v1.27.4
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
re := regexp.MustCompile(`^(red|green)$`)
words := []string{"red", "green", "blue"}
var matched []string
for _, w := range words {
if re.MatchString(w) {
matched = append(matched, w)
}
}
fmt.Println(strings.Join(matched, ","))
}
red,green
Java v27.0.1
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern p = Pattern.compile("^(red|green)$");
String[] words = {"red", "green", "blue"};
List<String> matched = new ArrayList<>();
for (String w : words) {
if (p.matcher(w).matches()) {
matched.add(w);
}
}
System.out.println(String.join(",", matched));
}
}
red,green
JavaScript v26.13.1
const words = ["red", "green", "blue"];
console.log(words.filter((w) => /^(red|green)$/.test(w)).join(","));
red,green
PHP v8.6.0
$a = ["red", "green", "blue"];
echo implode(",", preg_grep("/^(red|green)$/", $a));
red,green
Python v3.15.2
import re
words = ["red", "green", "blue"]
print(",".join(w for w in words if re.match(r"^(red|green)$", w)))
red,green
Rust v1.99.1
fn main() {
let re = regex::Regex::new(r"^(red|green)$").unwrap();
let words = vec!["red", "green", "blue"];
let matched: Vec<&str> = words
.into_iter()
.filter(|w| re.is_match(w))
.collect(); // filter keeps the matching elements (preg_grep semantics)
println!("{}", matched.join(","));
}
red,green
TypeScript v26.13.1
const words = ["red", "green", "blue"];
console.log(words.filter((w) => /^(red|green)$/.test(w)).join(","));
red,green
Language differences
C: POSIX regexec per element (search semantics with ^...$ anchors).
C++: regex_match (a full-string match by definition, no anchors needed).
C#: LINQ Where + Regex.IsMatch (search semantics, so anchors are required).
Go: a loop with MatchString.
Java: matcher().matches() (a full match by definition).
JavaScript: filter + test.
PHP: preg_grep matches each array element against the pattern and returns the matching subset (original keys preserved; PREG_GREP_INVERT selects non-matching elements).
Python: every language uses a filter idiom — re.match anchors at the start (with $ that means a full match) + generator.
Rust: filter + is_match (search semantics, anchors needed too).
TypeScript: same as JS — filter + test.