On Jun 28, Peter Scott said:
>>I want to delete a string but only the last.
>>Example
>>
>>I am the titi last human on titi earth.
>>
>>-> I want to erase the last "titi" nut not the other.
>
>No-one in my mailbox has mentioned a more obvious solution for the regex
>beginner:
>
>s/(.*)titi/$1/
Well, herein lies the problem.
$_ = "haha, he said, hahaha";
1: ####
2: ####
If I want to remove the last "haha", which one do I remove? The
"haha" before the "ha" (1), or the "haha" after the "ha" (2)?
Here are several means of doing either:
# removes 1
s/haha(?!.*haha)//s;
# removes 1 (requires Perl 5.6)
$_ = reverse;
my $p;
while (/(?=(ahah))/g) {
last if $p and $-[1] > $p->[1];
$p = [ $-[1], $+[1] ];
}
substr($_, $p->[0], $p->[1] - $p->[0]) = "";
$_ = reverse;
# removes 1
s/((?:haha.*?)*)haha/$1/s;
# removes 2
s/(.*)haha/$1/s;
# removes 2
($_ = reverse) =~ s/ahah//;
$_ = reverse;
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **