0

When using the C++(msvc compiler) regular expressions library(boost and stl), I have found that many of Online Tested(all from the Regex Stack Overflow FAQ) regular Expressions do not work, For Example:

\w
([\w]+)
[a-z]
[abcdef] \\etc... 

I am using Raw Literals for my regex's So that's not the problem.

The Code i am currently using to test them is

string somestring="something othersomething";
regex Test(R"(\w)",boost::regex_constants::JavaScript);
smatch match;
if(regex_match(somestring,match,Test))cout<<"True"<<endl;
else cout<<"false"<<endl;

When I try it in different Languages like Java this works why doesn't it C++?

P.S I have a feeling it is something to do with reading white spaces differently.

  • Different engines and flavours. That said, are you using gcc 4.7/4.8? Then see [this thread](http://stackoverflow.com/questions/12530406/is-gcc-4-7-and-gcc-4-8-buggy-about-regular-expressions) – HamZa Mar 21 '15 at 21:23
  • No Tested it on msvc 2011,2013,2015 - as i have those compilers – Ethan Riley Mar 21 '15 at 21:27
  • Have you tried `(\\w)` instead of `(\w)`? – HamZa Mar 21 '15 at 21:28
  • 1
    Using Literals everything in R"()" is taken literally no need for extra slash – Ethan Riley Mar 21 '15 at 22:33
  • 1
    `std::regex_match` (and I assume `boost::regex_match` as well) requires the entire input string to match the regex, not just part of it. Therefore your examples will only match exactly one character long strings (except `([\w]+)` which will match exactly one word). http://ideone.com/JrM6Rw – bcrist Mar 21 '15 at 23:00
  • This was rather a stupid question -sigh- did't research enough when i thought i did. oh well... – Ethan Riley Mar 22 '15 at 16:40

2 Answers2

0

Regular Expression engines vary between different programming languages. These are unofficially known as 'flavors'.

Java Regex != C++ Regex != PRCE != .NET Regex ...you get the picture.

If you are going to use an online regex tester, make sure it's for the flavor of regex you plan on using.

JNYRanger
  • 6,829
  • 12
  • 53
  • 81
  • The posted examples are trivial (`\w`, `[a-z]` etc), they mostly behave the same in all flavors... – Lucas Trzesniewski Mar 21 '15 at 22:36
  • Sadly for me they don't :( Anyway Do you mean `c++(ECMAScript) != Java(ECMAScript) != Boost(ECMAScript) != Online Tester(ECMAScript)` Beacuse that is what i am getting or do you mean `JavaRegex != C++ Regex` what scope? – Ethan Riley Mar 22 '15 at 16:00
  • They do mostly behave in similar ways, but it would make most sense to use the proper tester for the engine being used. Also, your examples don't exactly make sense because none of those are ECMAScript, which is a standardization of JavaScript. You want to make sure you use a tester that matches the library you are using. Also, note that the C++ built in Regex library was not fully implemented until GCC 4.9.0. – JNYRanger Mar 22 '15 at 16:11
  • Also the boost regex library is different from the c++ standard regex library – JNYRanger Mar 22 '15 at 16:16
  • Used A JavaScript Tester, Visual Studio 2012-2015 And Used the Syntax Refrence http://www.cplusplus.com/reference/regex/ECMAScript/ Although I still pretty new to regex for that matter – Ethan Riley Mar 22 '15 at 16:34
0

As JNYRanger Said there is a matter of flavors but If you have noticed I used regex_match not regex_search in my code

which is why the program was giving me a false Match Every time. Simply because I assumed that it would only match parts of a string, like the online Testers do and Other Languages.