-3

I want to match following string but receive always false and match is empty. Only if the expression is equal to the string the return value is true. My regex seems to be correct. What am I doing wrong?

#include <regex>

using namespace std;

int main()
{
   regex rgx("use_gui=((true|false)); state=((bla|blub|blob))");
   cmatch cm;
   string s("use_gui=false; state=bla");
   bool ret = regex_match(s.c_str(), cm, rgx);

}
alex555
  • 1,676
  • 4
  • 27
  • 45

3 Answers3

2

Consider removing the extra parenthesis:

regex rgx("use_gui=(true|false); state=(bla|blub|blob)");

This fixed it for me. (Thanks @Lightness Races in Orbit for showing me coliru)

Julien B.
  • 88
  • 6
1

The code you've written matches correctly for me and matches the string. I recommend not using the double parentheses, which are not necessary and may be confusing the regex engine that you're using:

int main()
{
   regex rgx("use_gui=(true|false); state=(bla|blub|blob)");
   cmatch cm;
   string s("use_gui=false; state=bla");
   bool ret = regex_match(s.c_str(), cm, rgx);
   cout << "matches: " << cm.size() << endl;
   for (cmatch::iterator p = cm.begin(); p != cm.end(); ++p) {
     cout << "match: " << *p << endl;
   }
}

On my machine (OSX 10.9, XCode) this produces:

matches: 3
match: use_gui=false; state=bla
match: false
match: bla
Tim Pierce
  • 5,514
  • 1
  • 15
  • 31
1

So your regex is basically valid, despite the superfluous capture groups. There must be something you're not showing us.

Unfortunately, in GCC 4.8 you get a = at the start of each match, but regexes are fixed properly in GCC 4.9.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055