Why am I not Able to Construct This Regex

Jonathan Mee

I wrote this regex:

(.+?)\s*{?\s*(.+?;)\s*}?\s*

Which tests fine: https://regex101.com/r/gD2eN7/1

But when I try to construct it in C++ I get a runtime-error.

Unhandled exception at 0x7714C52F in temp2.exe: Microsoft C++ exception:
std::regex_error at memory location 0x003BF2EC.

const auto input = "if (KnR)\n\tfoo();\nif (spaces) {\n    foo();\n}\nif (allman)\n{\n\tfoo();\n}\nif (horstmann)\n{\tfoo();\n}\nif (pico)\n{\tfoo(); }\nif (whitesmiths)\n\t{\n\tfoo();\n\t}"s;

cout << input << endl;

cout << regex_replace(input, regex("(.+?)\\s*{?\\s*(.+?;)\\s*}?\\s*"), "$1 {\n\t$2\n}\n") << endl;

Live Example

Am I using functionality that C++ doesn't support?

Wiktor Stribiżew

You need to escape the curly braces. See the std::regex ECMAScript flavor reference:

\character
The character character as it is, without interpreting its special meaning within a regex expression. Any character can be escaped except those which form any of the special character sequences above.
Needed for: ^ $ \ . * + ? ( ) [ ] { } |

regex_replace(input, regex("(.+?)\\s*\\{?\\s*(.+?;)\\s*\\}?\\s*"), "$1 {\n\t$2\n}\n")

Here is an IDEONE demo

#include <iostream>
#include <regex>
#include <string>

using namespace std;

int main() {
    const auto input = "if (KnR)\n\tfoo();\nif (spaces) {\n    foo();\n}\nif (allman)\n{\n\tfoo();\n}\nif (horstmann)\n{\tfoo();\n}\nif (pico)\n{\tfoo(); }\nif (whitesmiths)\n\t{\n\tfoo();\n\t}"s;

    cout << regex_replace(input, regex("(.+?)\\s*\\{?\\s*(.+?;)\\s*\\}?\\s*"), "$1 {\n\t$2\n}\n") << endl;
    //                                           ^^                ^^
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why am I not able to open a VS project

From Dev

Why am I not able to wget these types of links?

From Dev

Why am I not able to change worksheet reference?

From Dev

Why am I not able to add a JPanel to a JPanel?

From Dev

Why am I not able to use $this is an opencart helper?

From Dev

Why am I able to open this file?

From Dev

Why am I not able to add this route?

From Dev

Why am I not able to buy applications?

From Dev

With CTE why i am not able to print 1 to 1000 number or more but 1 to 100 i am able to print

From Dev

Why am i not able to print the whole character as i have entered it?

From Dev

Why am I not able to consistently have a `HashTable` serialized?

From Dev

Why I am not able to populate the json data in bootstrap table

From Dev

Why am I able to print this field in a static method?

From Dev

Why am I not being able to compile SASS with Webpack?

From Dev

Why am I able to instantiate my Abstract Base Class in Python?

From Java

Why am I able to edit a LINQ list while iterating over it?

From Dev

Why I am not able to update to the lastest iOS Cordova Platform?

From Dev

Why i am not able to pass optional value ( ? ) for getting image url?

From Dev

Why am I not able to initialize Generic Parameter and Array?

From Dev

Why am I not able to use FindResource() in Windows phone programming?

From Dev

Why am I not able to type input into input boxes?

From Dev

Why am I not able to load the required libraries before running the JAR?

From Dev

Why am I not being able to connect to my database?

From Dev

Why am I not able to untick this checkbox using JavaScript?

From Dev

Why am I not able to use stopwatch.Restart()?

From Dev

Why am I able to dereference and modify an `int * const` pointer?

From Dev

Why am I not able to use relative path to open a file in CLion?

From Dev

Why am I able to link without including ctype.h

From Dev

Why am I able to inherit & call a private constructor in a subclass?

Related Related

  1. 1

    Why am I not able to open a VS project

  2. 2

    Why am I not able to wget these types of links?

  3. 3

    Why am I not able to change worksheet reference?

  4. 4

    Why am I not able to add a JPanel to a JPanel?

  5. 5

    Why am I not able to use $this is an opencart helper?

  6. 6

    Why am I able to open this file?

  7. 7

    Why am I not able to add this route?

  8. 8

    Why am I not able to buy applications?

  9. 9

    With CTE why i am not able to print 1 to 1000 number or more but 1 to 100 i am able to print

  10. 10

    Why am i not able to print the whole character as i have entered it?

  11. 11

    Why am I not able to consistently have a `HashTable` serialized?

  12. 12

    Why I am not able to populate the json data in bootstrap table

  13. 13

    Why am I able to print this field in a static method?

  14. 14

    Why am I not being able to compile SASS with Webpack?

  15. 15

    Why am I able to instantiate my Abstract Base Class in Python?

  16. 16

    Why am I able to edit a LINQ list while iterating over it?

  17. 17

    Why I am not able to update to the lastest iOS Cordova Platform?

  18. 18

    Why i am not able to pass optional value ( ? ) for getting image url?

  19. 19

    Why am I not able to initialize Generic Parameter and Array?

  20. 20

    Why am I not able to use FindResource() in Windows phone programming?

  21. 21

    Why am I not able to type input into input boxes?

  22. 22

    Why am I not able to load the required libraries before running the JAR?

  23. 23

    Why am I not being able to connect to my database?

  24. 24

    Why am I not able to untick this checkbox using JavaScript?

  25. 25

    Why am I not able to use stopwatch.Restart()?

  26. 26

    Why am I able to dereference and modify an `int * const` pointer?

  27. 27

    Why am I not able to use relative path to open a file in CLion?

  28. 28

    Why am I able to link without including ctype.h

  29. 29

    Why am I able to inherit & call a private constructor in a subclass?

HotTag

Archive