June 2025
Intermediate to advanced
1093 pages
33h 24m
English
C++ now natively supports regular expressions. With them, you can search for patterns in strings and extract parts of them.
// https://godbolt.org/z/T1YMoM5as#include <string>#include <iostream>#include <regex>using std::regex; using std::sregex_iterator; using std::string;const regex rgxMobile(R"(01[567]\d{6,10})"); // Mobile phone 0151-0179bool isMobilephone(const string& text) { return std::regex_match(text, rgxMobile); // Does the text match completely?}bool containsMobilephone(const string &text) { return std::regex_search(text, rgxMobile); // somewhere in the text?}void listMobilephones(const string &text) { sregex_iterator begin{ text.cbegin(), text.cend(), rgxMobile }; sregex_iterator end; for(auto it = begin; ...
Read now
Unlock full access