From: Edward Diener (eddielee_at_[hidden])
Date: 2003-08-07 17:56:55
Ross Smith wrote:
> George A. Heintzelman 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?
>> Second that request. I just had a need for this, though I wound up
>> ignoring the problem rather than fixing it...
> There must be something in the air; I just had a need for this too. My
> quick-and-dirty solution was to simply replace every non-alphanumeric
> character with a hex escape sequence (\xNN).
> Having it just add escapes to a list of special characters wouldn't
> work as a general solution, because the list of characters depends on
> flags passed to the regex functions.
That is a good point. However you always know what flags are passed to the
regex function since they are available. That's not arguing against such a
function as being part of the library itself but it is still doable by you.
Furthermore you can set the flag mode you like to simplify your escaping,
but of course this is not a general solution as the rest of the regular
expression may require particular flags set.
> Of course there are settings
> my hex-escape solution won't work either. I don't think a general
> solution is possible without making it part of the library; it needs
> access to the innards of the regex objects in order to know the exact
> syntax they recognise.
That is not true. One just needs to read and understand the documentation on
the particular flags and what they mean. There are no "secret innards" which
retain information about escaping characters which you can't find for
yourself by testing the flags.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk