Regular Expressions
This page is under construction. Please come back later.
#include "Utils.hpp" #include <clocale> #include <codecvt> #include <iostream> #include <regex> #include <string> #include <vector> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); wstring const s = L"Four score and seven years ago..."; wcout << L"Match 1: " << regex_search(s, wregex(L"s.*e")) << endl; wcout << L"Match 2: " << regex_search(s, wregex(L"\\bs.*e\\b")) << endl; wcout << L"Match 3: " << regex_search(s, wregex(L"s...e")) << endl; wcout << L"Match 4: " << regex_search(s, wregex(L"b.d")) << endl; vector<wstring> subgroups = Utils::regex_findfirst(wregex(L"(\\w+)\\s*(\\w+)"), s); wcout << L"Find First (with subgroups): " << Utils::to_wstring(subgroups) << endl; subgroups = Utils::regex_findfirst(wregex(L"bad match"), s); wcout << L"Find First (bad match): " << Utils::to_wstring(subgroups) << endl; vector<wstring> matches = Utils::regex_findall(wregex(L"\\w+"), s); wcout << L"Find All: (matches only)" << Utils::to_wstring(matches) << endl; matches = Utils::regex_findall(wregex(L"bad match"), s); wcout << L"Find All (bad match): " << Utils::to_wstring(matches) << endl; return 0; }
Output
$ g++ -std=c++17 RegEx1.cpp -o RegEx1 -lfmt
$ ./RegEx1
Match 1: 1
Match 2: 1
Match 3: 1
Match 4: 0
Find First (with subgroups): ["Four score", "Four", "score"]
Find First (bad match): []
Find All: (matches only)["Four", "score", "and", "seven", "years", "ago"]
Find All (bad match): []
#include "Utils.hpp" #include <clocale> #include <codecvt> #include <iostream> #include <regex> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); wstring const s = L"Four score and seven years ago..."; wcout << L"Replace First 1: " << std::regex_replace(s, wregex(L"\\.\\.\\."), L"!!!", regex_constants::format_first_only) << endl; wcout << L"Replace First 2: " << std::regex_replace(s, wregex(L"\\b...\\b"), L"???", regex_constants::format_first_only) << endl; wcout << L"Replace First 3: " << std::regex_replace(s, wregex(L"b.d"), L"???", regex_constants::format_first_only) << endl; wcout << L"Replace First 4: " << std::regex_replace(s, wregex(L"(\\w+) (\\w+)"), L"$2 $1", regex_constants::format_first_only) << endl; wcout << L"Replace All 1: " << std::regex_replace(s, wregex(L"\\b...\\b"), L"???") << endl; wcout << L"Replace All 2: " << std::regex_replace(s, wregex(L"(\\w+) (\\w+)"), L"$2 $1") << endl; return 0; }
Output
$ g++ -std=c++17 RegEx2.cpp -o RegEx2 -lfmt
$ ./RegEx2
Replace First 1: Four score and seven years ago!!!
Replace First 2: Four score ??? seven years ago...
Replace First 3: Four score and seven years ago...
Replace First 4: score Four and seven years ago...
Replace All 1: Four score ??? seven years ???...
Replace All 2: score Four seven and ago years...
#include "Utils.hpp" #include <clocale> #include <codecvt> #include <iostream> #include <regex> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); wstring const str = L"Four score and seven years ago..."; wcout << L"Split 1: " << Utils::to_wstring(Utils::split(wregex(L" "), str)) << endl; wcout << L"Split 2: " << Utils::to_wstring(Utils::split(wregex(L"[eo]"), str)) << endl; wcout << L"Split 3: " << Utils::to_wstring(Utils::split(wregex(L"\\s"), str)) << endl; wcout << L"Split 4: " << Utils::to_wstring(Utils::split(wregex(L"\\W"), str)) << endl; return 0; }
Output
$ g++ -std=c++17 RegEx3.cpp -o RegEx3 -lfmt
$ ./RegEx3
Split 1: ["Four", "score", "and", "seven", "years", "ago..."]
Split 2: ["F", "ur sc", "r", " and s", "v", "n y", "ars ag", "..."]
Split 3: ["Four", "score", "and", "seven", "years", "ago..."]
Split 4: ["Four", "score", "and", "seven", "years", "ago", "", ""]
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
-
[[C++ Programming Language]], 4th Edition, Bjarne Stroustrup, Addison-Wesley, 2013, ISBN 978-0321563842.
- [[C++ Language Reference]]
- [[cplusplus.com]]
- [[Cprogramming.com]]