Skip to main content
Sync with the original answer
Source Link
nhahtdh
  • 56.9k
  • 15
  • 131
  • 164

Using code from Dav (+ a fix from comments), I created ASCII/Unicode function regex_escape()regex_escape():

std::wstring regex_escape(const std::wstring& string_to_escape) { static const boost::wregex re_boostRegexEscape( _T("[\\^\\"[.\\$\\|\\^$|(\\)\\[\\]\\*\\+\\\\[\\]{}*+?\\/\\\\]") ); const std::wstring rep( _T("\\\\\\1&""\\\\&") ); std::wstring result = regex_replace(string_to_escape, re_boostRegexEscape, rep, boost::match_default | boost::format_sed); return result; } 

For ASCII version, use std::string std::string/ boost::regexboost::regex instead of std::wstring std::wstring/ boost::wregexboost::wregex.

Using code from Dav (+ a fix from comments), I created ASCII/Unicode function regex_escape():

std::wstring regex_escape(const std::wstring& string_to_escape) { static const boost::wregex re_boostRegexEscape( _T("[\\^\\.\\$\\|\\(\\)\\[\\]\\*\\+\\?\\/\\\\]") ); const std::wstring rep( _T("\\\\\\1&") ); std::wstring result = regex_replace(string_to_escape, re_boostRegexEscape, rep, boost::match_default | boost::format_sed); return result; } 

For ASCII version, use std::string / boost::regex instead of std::wstring / boost::wregex.

Using code from Dav (+ a fix from comments), I created ASCII/Unicode function regex_escape():

std::wstring regex_escape(const std::wstring& string_to_escape) { static const boost::wregex re_boostRegexEscape( _T("[.^$|()\\[\\]{}*+?\\\\]") ); const std::wstring rep( _T("\\\\&") ); std::wstring result = regex_replace(string_to_escape, re_boostRegexEscape, rep, boost::match_default | boost::format_sed); return result; } 

For ASCII version, use std::string/boost::regex instead of std::wstring/boost::wregex.

Source Link
Nishi
  • 10.8k
  • 3
  • 31
  • 37

Using code from Dav (+ a fix from comments), I created ASCII/Unicode function regex_escape():

std::wstring regex_escape(const std::wstring& string_to_escape) { static const boost::wregex re_boostRegexEscape( _T("[\\^\\.\\$\\|\\(\\)\\[\\]\\*\\+\\?\\/\\\\]") ); const std::wstring rep( _T("\\\\\\1&") ); std::wstring result = regex_replace(string_to_escape, re_boostRegexEscape, rep, boost::match_default | boost::format_sed); return result; } 

For ASCII version, use std::string / boost::regex instead of std::wstring / boost::wregex.