Boost logo

Boost :

From: Edward Diener (eddielee_at_[hidden])
Date: 2003-08-08 09:48:22


John Maddock wrote:
>> Given that I have a string 's' from somewhere, I'd like to create a
>> regular expression where some part must match that string. The
>> problem
>> is, the 's' could contain characters that have a special meaning in
>> regular expressions. Is there some support function that can provide
>> an escaped version of 's'? Something that transforms "my.*string"
>> into "my\.\*string"? If there isn't, would it be possible/easy to
>> provide one?
>
> Good question, no there isn't, but how about:
>
> std::string escape_regex(const std::string& s)
> {
> static const std::regex e("[\\[\\]$\\^|.+*?(){}]");
> return regex_merge(s, e, "\\\\$&");
> }
>
> Just off the top of my head and untried....
>
> I'll try and think up something more general that works with all the
> flag settings though...

Front end localization could change this also, I believe. For instance if a
dll or message catalog substitutes '!' for '$' wouldn't I need to escape '!'
instead of '$' in order to use '!' as a literal in an expression ?

In this regard it would be helpful if the end-user could obtain back at
run-time the substitutions that are made due to localization. I didn't see
this as a function of the regex_traits class reference but maybe it is
there.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk