Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Regular expression confusion (std::regex_replace).

Regular expression confusion (std::regex_replace).

Scheduled Pinned Locked Moved C / C++ / MFC
regexcomquestion
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    (the pre tag seems to be broken with the code) const std::wstring expression( L"Value stored = $(_VARIABLE_NAME_)"); const std::wstring tmp(LR"(\$\(([^\(\)\$]*?)\))"); const std::wregex rx(tmp); const std::wstring variableValue(L"A2347X$01GBFC"); std::match_results matchResult; const bool found = std::regex_search(expression, matchResult, rx); if ( found ) { const std::wstring expressionOut = std::regex_replace(expression, rx, variableValue, std::regex_constants::format_first_only); std::wcout << "expressionOut = " << expressionOut << "!\n"; } When I run the above code (or use [Regex Tester](https://www.regextester.com/) ) (I added the bold) expressionOut = Value stored = A2347X**_VARIABLE_NAME_**GBFC I was expecting (or I am wanting) the result to be expressionOut = Value stored = A2347X$01GBFC Is there a way to treat the input "A2347X$01GBFC" as a "raw" input and not something to be parsed by the std::regex_replace ? In our case, the string "A2347X$01GBFC" is something that a user can input manually. I'm certain there is an way to do it, but I cannot see it. Thanks.

    I'd rather be phishing!

    K J 2 Replies Last reply
    0
    • M Maximilien

      (the pre tag seems to be broken with the code) const std::wstring expression( L"Value stored = $(_VARIABLE_NAME_)"); const std::wstring tmp(LR"(\$\(([^\(\)\$]*?)\))"); const std::wregex rx(tmp); const std::wstring variableValue(L"A2347X$01GBFC"); std::match_results matchResult; const bool found = std::regex_search(expression, matchResult, rx); if ( found ) { const std::wstring expressionOut = std::regex_replace(expression, rx, variableValue, std::regex_constants::format_first_only); std::wcout << "expressionOut = " << expressionOut << "!\n"; } When I run the above code (or use [Regex Tester](https://www.regextester.com/) ) (I added the bold) expressionOut = Value stored = A2347X**_VARIABLE_NAME_**GBFC I was expecting (or I am wanting) the result to be expressionOut = Value stored = A2347X$01GBFC Is there a way to treat the input "A2347X$01GBFC" as a "raw" input and not something to be parsed by the std::regex_replace ? In our case, the string "A2347X$01GBFC" is something that a user can input manually. I'm certain there is an way to do it, but I cannot see it. Thanks.

      I'd rather be phishing!

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      I can't see it either. It seems you've got two options, either use format_sed, if you know that you wont have \1 in the replacement string, or you'll have to turn any single $ into $$ before passing in to regex_replace There really should be a format_no_format flag to turn off format replacements.

      M 2 Replies Last reply
      0
      • M Maximilien

        (the pre tag seems to be broken with the code) const std::wstring expression( L"Value stored = $(_VARIABLE_NAME_)"); const std::wstring tmp(LR"(\$\(([^\(\)\$]*?)\))"); const std::wregex rx(tmp); const std::wstring variableValue(L"A2347X$01GBFC"); std::match_results matchResult; const bool found = std::regex_search(expression, matchResult, rx); if ( found ) { const std::wstring expressionOut = std::regex_replace(expression, rx, variableValue, std::regex_constants::format_first_only); std::wcout << "expressionOut = " << expressionOut << "!\n"; } When I run the above code (or use [Regex Tester](https://www.regextester.com/) ) (I added the bold) expressionOut = Value stored = A2347X**_VARIABLE_NAME_**GBFC I was expecting (or I am wanting) the result to be expressionOut = Value stored = A2347X$01GBFC Is there a way to treat the input "A2347X$01GBFC" as a "raw" input and not something to be parsed by the std::regex_replace ? In our case, the string "A2347X$01GBFC" is something that a user can input manually. I'm certain there is an way to do it, but I cannot see it. Thanks.

        I'd rather be phishing!

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        Interesting. You could drop the capture group by eliminating the parentheses before the square bracket and after the question mark. Edit: It seems the more correct way is to create a non-capturing group by adding "?:" after the group's opening parentheses:

        const std::wstring tmp(LR"(\$\((?:[^\(\)\$]*?)\))");

        Added: You could also scan the input and double up any dollar signs.

        M 1 Reply Last reply
        0
        • K k5054

          I can't see it either. It seems you've got two options, either use format_sed, if you know that you wont have \1 in the replacement string, or you'll have to turn any single $ into $$ before passing in to regex_replace There really should be a format_no_format flag to turn off format replacements.

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          Awesome! Thanks. Will try monday.

          I'd rather be phishing!

          1 Reply Last reply
          0
          • J Joe Woodbury

            Interesting. You could drop the capture group by eliminating the parentheses before the square bracket and after the question mark. Edit: It seems the more correct way is to create a non-capturing group by adding "?:" after the group's opening parentheses:

            const std::wstring tmp(LR"(\$\((?:[^\(\)\$]*?)\))");

            Added: You could also scan the input and double up any dollar signs.

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            Awesome! Thanks. Will try monday.

            I'd rather be phishing!

            1 Reply Last reply
            0
            • K k5054

              I can't see it either. It seems you've got two options, either use format_sed, if you know that you wont have \1 in the replacement string, or you'll have to turn any single $ into $$ before passing in to regex_replace There really should be a format_no_format flag to turn off format replacements.

              M Offline
              M Offline
              Maximilien
              wrote on last edited by
              #6

              The format_sed option seems to work; I will make the changes and push that to the test suites. Thanks.

              I'd rather be phishing!

              K 1 Reply Last reply
              0
              • M Maximilien

                The format_sed option seems to work; I will make the changes and push that to the test suites. Thanks.

                I'd rather be phishing!

                K Offline
                K Offline
                k5054
                wrote on last edited by
                #7

                Be aware that sed_format uses "\0" to replace matches. In your case if the user input was "A2347\01GBFC", you'd get the same output as for "A2347X$01GBFC" with format_default. If that's not an issue, then you're good to go.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups