1

Given that this code works:

regex r1{ "fish"s };
smatch m1;
if (regex_search("I love fish and chips"s, m1, r1))
    cout << m1[0] << endl;

I believe that VS2015 supports regular expressions. However, initialization of this regular expression object:

regex r{ R"(\d{2,3}(-\d\d) { 2 })" };

throws a std::regex_error exception. What's wrong with the initialization?

4
  • 1
    Try to replace { 2 } (plus the space left from {) with {2} or - with \-. Commented Mar 30, 2015 at 17:28
  • @Xufox wow, dude, you are absolutely correct! This part of the regexp (-\d\d) { 2 } needed to be written as (-\d\d){2} Commented Mar 30, 2015 at 17:33
  • @Xufox thank you :) I spent a hour on this tiny tiny mistake. Commented Mar 30, 2015 at 17:35
  • 1
    @NikolayZhulikov: Just for your knowledge, GCC 4.9.0 supports <regex> Commented Mar 30, 2015 at 18:03

2 Answers 2

3

So, yeah, as mentioned in the comments:

(\d{2,3}(-\d\d) { 2 })

should be

(\d{2,3}(-\d\d){2})

otherwise the {2} relates to the space instead of the (-\d\d), and other weird things might possibly happen as well…

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

Comments

2

You have a typo in your regex. Change this:

regex r{ R"(\d{2,3}(-\d\d) { 2 })" };

To:

regex r{ R"(\d{2,3}(-\d\d){2})" };

Comments

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.