Robson
Robson

Reputation: 926

how to escape a whole sequence of characters?

I'm using the following code to "execute" a regular expression in mfc c++

std::wregex rx(regularExpression,nFlags);
std::wcmatch res;
std::regex_search(inputString, res, rx);

This is used in a dialog where the user types in the regularExpression and I search it in my inputString.

The user might want to not use a regular expression to find what he wants, in that case I provide an option "whole word" that will match any character in the regularExpression variable.

The "whole word" is done by adding enclosing regularExpression in < and >, is there any other special character that will make the regex_search method ignore every regex special character, treating them literally, that is enclosed by < and >?

Upvotes: 0

Views: 167

Answers (1)

bames53
bames53

Reputation: 88215

so how can I escape the whole string without having to escape every single special character?

C++ regex does not support any way to do this. You will have to escape each special character if you're going to pass the user's string to the regex matcher.

In some regex engines there are multiple modes, where one mode treats special characters specially by default and escaping them causes them to behave literally, and another mode where characters behave literally by default and have to be escaped in order to behave specially. However C++ regex doesn't support that.

Upvotes: 2

Related Questions