Regular expression confusion (std::regex_replace).
-
(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!
-
(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!
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 toregex_replace
There really should be aformat_no_format
flag to turn off format replacements. -
(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!
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.
-
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 toregex_replace
There really should be aformat_no_format
flag to turn off format replacements.Awesome! Thanks. Will try monday.
I'd rather be phishing!
-
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.
Awesome! Thanks. Will try monday.
I'd rather be phishing!
-
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 toregex_replace
There really should be aformat_no_format
flag to turn off format replacements.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!
-
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!