Regex with STD :: REGEX in C ++

0

I'm developing a C ++ system that needs to get some information from a String using regular expression, I'm using a regular expression that I've used in PHP perfectly, but in C ++ it returns blank.

const std::string s = "{{ teste }}";
std::string r = "{{((.)+)}}";

std::regex rgx(r);
std::smatch match;

if (std::regex_search(s.begin(), s.end(), match, rgx)){
    std::cout << "Number of maths: " << match.size() << "\n";
    for(int i=0; i < match.size(); i++){
        std::cout << "match: '" << match[i] << "'\n";
    }
}

In PHP it looked like this and it worked perfectly:

$print_value = '/{{((.)+)}}/';
$var_get = "{{ teste }}";
preg_match($print_value, $var_get, $matches, PREG_OFFSET_CAPTURE);
$piece = explode($formatches[0][0], $var_get);
.
.
.

What is going wrong? Other expressions I use work perfectly.
Thank you in advance.

    
asked by anonymous 14.04.2016 / 00:32

1 answer

1

What's going wrong? PHP , this dynamic language makes everything very simple, and even fixes errors that you may not even realize.

Problem

  • { and } in REGEX is a reserved character >, what PHP should be doing is an automatic cast for literal, because after { is expected a 0-9 quantifier.

PHP should be interpreting your REGEX as /\{\{((.)+)\}\}/ for this reason.

Solution

Change your REGEX in C ++ (type language) to \{\{((.)+)\}\} .

Note

  • No need to create (.)+ , if your intention is to pegal any character, so you're just creating one more group. Just .+ .
14.04.2016 / 01:09