Nicolas Charvoz
Nicolas Charvoz

Reputation: 1509

Regex in C++11 vs PHP

I'm new to regex and C++11. In order to match an expression like this :

TYPE SIZE NUMBER ("regina s x99");

I built a regex which looks like this one :

\b(regina|margarita|americaine|fantasia)\b \b(s|l|m|xl|xxl)\b x([1-9])([0-9])

In my code I did this to try the regex :

std::string s("regina s x99");
std::regex rgx($RGX); //$RGX corresponds to the regex above
if (std::regex_match(s, rgx))
std::cout << "It works !" << std::endl;

This code throw a std::regex_error, but I don't know where it comes from..

Thanks,

Upvotes: 1

Views: 447

Answers (3)

Steephen
Steephen

Reputation: 15824

There was a typo in this line in original question:

if (std::reegex_match(s, rgx))

More over I am not sure what are you passing with this variable : $RGX

Corrected program as follows:

#include<regex>
#include<iostream>
int main()
{
    std::string s("regina s x99");
std::regex rgx("\\b(regina|margarita|americaine|fantasia)\\b \\s*(s|l|m|xl|xxl)\\b \\s*x([1-9])([0-9])"); //$RGX corresponds to the regex above
if (std::regex_match(s, rgx))
std::cout << "It works !" << std::endl;
else
  std::cout<<"No Match"<<std::endl;
}

Upvotes: 0

jpw
jpw

Reputation: 44881

This works with g++ (4.9.2) in c++11 mode:

std::regex rgx("\\b(regina|margarita|americaine|fantasia)\\b\\s*(s|l|m|xl|xxl)\\b\\s*x([1-9]*[0-9])"); 

This will capture three groups: regina s 99 which matches the TYPE SIZE NUMBER pattern, while your original captured four groups regina s 9 9 and had the NUMBER as two values (maybe that was what you wanted though).

Demo on IdeOne

Upvotes: 1

Jonathan Wakely
Jonathan Wakely

Reputation: 171263

In C++ strings the \ character is special and needs to be escaped so that it gets passed to the regular expression engine, not interpreted by the compiler.

So you either need to use \\b:

std::regex rgx("\\b(regina|margarita|americaine|fantasia)\\b \\b(s|l|m|xl|xxl)\\b x([1-9])([0-9])");

or use a raw string, which means that \ is not special and doesn't need to be escaped:

 std::regex rgx(R"(\b(regina|margarita|americaine|fantasia)\b \b(s|l|m|xl|xxl)\b x([1-9])([0-9]))");

Upvotes: 1

Related Questions