Add wxRegEx::QuoteMeta() helper

Quote all characters special for wxRegEx in the given string: this can
be useful when searching for a literal string using wxRegEx.

Closes https://github.com/wxWidgets/wxWidgets/pull/1489
This commit is contained in:
Tomay
2019-08-24 22:26:50 +01:00
committed by Vadim Zeitlin
parent 363cdc5fdc
commit 050ca4ce3a
4 changed files with 62 additions and 0 deletions

View File

@@ -250,5 +250,31 @@ public:
Replace the first occurrence.
*/
int ReplaceFirst(wxString* text, const wxString& replacement) const;
/**
Escapes any of the characters having special meaning for wxRegEx.
Currently the following characters are special: \\, ^, $, ., |, ?, *,
+, (, ), [, ], { and }. All occurrences of any of these characters in
the passed string are escaped, i.e. a backslash is inserted before
them, to remove their special meaning.
For example:
@code
wxString quoted = wxRegEx::QuoteMeta("foo.*bar");
assert( quoted == R"(foo\.\*bar)" );
@endcode
This function can be useful when using wxRegEx to search for a literal
string entered by user, for example.
@param str
A string that may contain metacharacters to escape.
@return A string with all metacharacters escaped.
@since 3.1.3
*/
static wxString QuoteMeta(const wxString& str);
};