Flags
Saving string in match results
Examples
regex_literal is a templated user defined literals wrapper for std::regex. "regex_search" and "regex_match" functions are packed into a new structure 'struct_rm' to be able to write regex request in only one line.
Both char and wchar_t are compatible. It can be extended using struct_rm template if needed.
To use regex_literals, include the header, type using namespace regex_literals;
and use _rg
after a string literal to transform it to a struct_rm.
struct_rm supports regex flags as "/regex/flags"_rg
which follows std::regex_constants::syntax_option_type flags. Here is available flags:
i
: icaseb
: nosubso
: optimizec
: collateE
: ECMAScriptB
: basicX
: extendedA
: awkG
: grepP
: egrep
C++17 flags compatibility:
m
: multiline
Flags can be disabled using #define REGEX_LITERALS_FLAGS 0
before including regex_literals.
Another feature is the capability to save string into match_results with match_results_ws
so a regex request doesn't need to get a string reference for match results (well... it still needs string reference but in this case, it's stored in match_results).
#include <iostream>
#include "regex_literals.h"
int main()
{
using namespace regex_literals;
/// simple match
if ("abc"_rg.match("abc"))
std::cout << "abc=abc: ok" << std::endl;
/// simple case sensitive match
if ("abc"_rg.match("ABC"))
std::cout << "abc=ABC: ok"<< std::endl;
/// simple match with case insensitive flag
if ("/abc/i"_rg.match("ABC"))
std::cout << "abc=ABC (insensitive case): ok"<< std::endl;
/// search
if ("/abc/i"_rg.search("xyzABC123"))
std::cout << "xyzABC123 contains abc (insensitive case): ok"<< std::endl;
/// get results
std::smatch sm;
std::string mystr=" xyzABC123 ";
if (R"(/\s*(\w+)\s*/i)"_rg.search(mystr, sm))
std::cout << "trim word: ok: '" << sm[1] << "'" << std::endl;
/// save string in match results and get results
match_results_ws<char> sm_ws;
if (R"(\s*(\w+)\s*)"_rg.search(" mystr ", sm_ws))
std::cout << "trim word: ok: '" << sm_ws[1] << "'" << std::endl;
if (R"(\s*(\w+)\s*)"_rg.search(" new_str ", sm_ws))
std::cout << "trim word: ok: '" << sm_ws[1] << "'" << std::endl;
return 0;
}