Skip to content
Derek Snider edited this page May 19, 2026 · 1 revision

Regex

Regular expressions via std::regex, available through the madc:: namespace. The perl:: namespace also provides regex-powered grep and split.

madc::regex_match — full string match

int m = madc::regex_match(s, "[A-Za-z]+ [A-Za-z]+ [0-9]+");
// returns 1 if the ENTIRE string matches, 0 otherwise

madc::regex_search — partial match

int found = madc::regex_search(s, "[0-9]+");
// returns 1 if the pattern matches ANYWHERE in the string

madc::regex_replace — substitution

string result;
madc::regex_replace(result, source, pattern, replacement);
// replaces all occurrences; original is not modified

Example

#include <iostream>

int main() {
    string s = "hello123";
    int m = madc::regex_match(s, "hello[0-9]+");
    cout << m << endl;            // 1

    string s2 = "foo bar baz";
    int found = madc::regex_search(s2, "bar");
    cout << found << endl;        // 1

    string result;
    madc::regex_replace(result, "The quick brown fox", "quick|brown", "slow");
    cout << result << endl;       // The slow slow fox

    return 0;
}

Regex in perl:: namespace

perl::grep filters arrays by regex match. perl::split splits strings by regex delimiter. Both fall back to substring matching if the pattern isn't a valid regex.

array words;
php::explode(words, ",", "apple,banana,cherry,avocado");
array matches;
perl::grep(matches, "^a", words);
// matches: apple, avocado

Invalid regex patterns are caught internally — no crash or exception propagates to your program.

What's next?

Clone this wiki locally