
negating a string in a regexp
Quote:
>A potentially stupid question, but I have tried to find out for myself
>and not met with any success, ie the faq, asking people etc. Maybe I
>just missed it... but
>how does one negate a string in a regexp?
That's a very difficult exercise, in general. Here's a s///g statement that
works for your particular example:
s/([^DT]T?|D[^T])+/{$&}/g;
But that approach is not very intuitive. A better (more maintainable)
approach might be to do this:
undef $a;
$a .= "{$`}$&", $_ = $' while /DT/;
$_ = "$a{$_}";
As you can see, you could pick a different /DT/ pattern easily. (Note: the
repeated /DT/ match is probably not very expensive because after each
iteration we remove the beginning of $_.)
Here are some sample runs:
foo D bar DT bar T foo -> {foo D bar }DT{ bar T foo}
aDTbDTcDT -> {a}DT{b}DT{c}DT{}
foo DDDDDDTTTTTT bar -> {foo DDDDD}DT{TTTTT bar}
DT -> {}DT{}
DDT -> {D}DT{}
DTDT -> {}DT{}DT{}
DDDDDD -> {DDDDDD}
DTD -> {}DT{D}
Michael.
--
"We are floating in a medium of vast extent, always drifting uncertainly,
blown to and fro; whenever we think we have a fixed point to which we can
cling and make fast, it shifts and leaves us behind; if we follow it, it
eludes our grasp, slips away, and flees eternally before us. Nothing stands
still for us. This is our natural state and yet the state most contrary to
our inclincations. We burn with desire to find a firm footing, an ultimate,
lasting base on which to build a tower rising up to infinity, but our whole
foundation cracks and the earth opens..." -- {*filter*}ia Woolf