0

Following regex comparison is failing, Please suggest what could be the reason.

std::string pattern = "(ABC '.*' and '.*' DEF)";
std::string val     = "(ABC '&' and '|' DEF)";

try
{
    boost::regex re(pattern, boost::regex::extended);
    if(boost::regex_match(val, re))
        cout << " matched"<< std::endl;
    else
        cout << " no match " << std::endl;
}
catch(...)
{
    cout << " Exception" << std::endl;
}

O/P -- no match

Pattern matches successfully with POSIX regex(regcomp) but with TCL regex also pattern doesn't match.

Please suggest what could be the reason.

anshul garg
  • 473
  • 3
  • 7
  • `regex_match` requires a full string match. Your regex does not match it all. Use `regex_search`. Or, escape the parentheses in the pattern to make them literal parentheses if you need the whole string match. In BRE POSIX, unescaped parentheses match literal parentheses and you need to escape them there to make capturing groups, see the answer in the linked dupe: *Escaping parentheses and curly brackets in BREs gives them the special meaning their unescaped versions have in EREs.* – Wiktor Stribiżew Nov 28 '18 at 19:34
  • @WiktorStribiżew I tried escaping parenthesis but still regex won't match. – anshul garg Nov 29 '18 at 17:10
  • No, it [matches fine](https://coliru.stacked-crooked.com/a/2229a0b41f1c5e3b) once the parentheses are escaped. – Wiktor Stribiżew Nov 29 '18 at 17:33

0 Answers0