Skip to content
Justice Tech School

Regular Expressions and Text

<regex> matches patterns with regex_match, regex_search and regex_replace.

Example
#include <regex>
regex re("(\\d+)-(\\d+)");
smatch m;
string s = "range 10-25 ok";
if (regex_search(s, m, re)) cout << m[1] << " to " << m[2] << "\n";
cout << regex_replace("a1b22", regex("\\d+"), "#") << "\n";
cout << regex_match("abc", regex("[a-c]+")) << "\n";

Result

10 to 25
a#b#
1

Exercise

Replace matches.

(text, re, "x")

Quiz Test

Which tests the whole string?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.