Re: regsub replacing the last off
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: regsub replacing the last off

From: Bryan Oakley <oakley@bardo.clearlight.com>
Date: Wed Dec 28 2005 - 18:58:48 CET

Andre Arpin wrote:
> why is this true
>
> % regsub {b(?:[^b]*$)} abcabcabc x
> abcabcax
>
> I would have expected
> abcabcaxc
>

Your pattern matches that final "bc" (b followed by zero or more
occurances of anything but b). Thus, regsub replaces what was matched
("bc") with the replacement.

 From the man page:

    "...the portion of string that matched exp is replaced..."

Since "bc" is what matched (which is different from what is captured),
that is what is replaced.

>
> Is there a way to replace the last b?

Capture the parts you don't want replaced in the matching pattern, and
include that in the substitution

regsub {b([^b]*$)} abcabcabc {x\1}
Received on Tue Jan 3 03:09:26 2006