2

I have this string to test:

"CPF char,
Nome char [80],
Idade int,
Salario double,
Sexo char,
Tem_Filhos bool,"

Or simply:

string testStatement("CPF char,\nNome char [80],\nIdade int,\nSalario double,\nSexo char,\nTem_Filhos bool,");

Here is my regEx:

regex createTableInside("(?:[ \\n\\t]*)(\\w*)(?:[ \\n\\t]+)(?:(?:(char)[ \\n\\t]*\\[[ \\n\\t]*(\\d*)[ \\t\\n]*\\])|(char)|(int)|(double)|(bool)|(bloob))(?:[ \\n\\t]*)(,)");

When i do a regex_search, it finds everything:

while(regex_search(testStatement, check, createTableInside))
{
    for(index = 1; index < check.size(); index++)
    {
        if(check.str(index).compare("") == 0) continue;

        cout << "\"" << check.str(index) << "\"" << endl;
    }

    aux = check.suffix();
}

Output:

"CPF"
"char"
","
"Nome"
"char"
"80"
","
"Idade"
"int"
","
"Salario"
"double"
","
"Sexo"
"char"
","
"Tem_Filhos"
"bool"
","

But when I do a regex_match, the check.size() always returns 0.

I've tested my regEx here: https://www.debuggex.com/, and it works.

I've also tried the match_any constant:

regex_match(testStatement, check, createTableInside, match_any);

Whats wrong?

Thanks.

5
  • Which compiler/library? Commented Feb 26, 2014 at 18:55
  • I'm using #include <regex> and the default mac osx g++ compiler Commented Feb 26, 2014 at 18:56
  • I'm not sure, but it could be that regex_search() will give you everything it possibly matches (groupwise), while regex_match() might want to match the complete input. Are you sure that the complete input matches the expression? Commented Feb 26, 2014 at 19:05
  • Maybe regex_match doesn't do sub-captures, so it just returns true or false, therefore no match info. Commented Feb 26, 2014 at 19:06
  • Put up the regex_match code you are using. And, it won't need a while(), just a if() Commented Feb 26, 2014 at 19:11

1 Answer 1

2

As from the docs (std::regex_search from cppreference.com):

std::regex_search will successfully match any subsequence of the given sequence, whereas std::regex_match will only return true if the regular expression matches the entire sequence.

You may want to inspect the std::match_results, to check at which point exactly std::regex_match() has failed to match the complete sequence (see std::match_results::suffix() and std::match_results::prefix() members).

Sign up to request clarification or add additional context in comments.

9 Comments

But it matches the entire sequence. I'm testing with regex101.com/#PCRE either. Using /g flag.
There are different regex engines, that may give you different results: 'online regex tester for javascript, php, pcre and python'!
So why it is not matching in c++ ?
@MaurícioGiordano: it does not match the entire sequence. Surrounding your whole regex with (?:...)+ would make it match the entire sequence.
@MaurícioGiordano Because there are different engines, as I mentioned. Also I'd recommend to use the regular regex character class shortcuts, such as '\w', etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.