in reply to Re: s/// only replacing most of the time...
in thread s/// only replacing most of the time...

I have given that some though. I just haven't worked out in my head how to deal with overlaps. However, I do agree that the way I'm doing it now is really really overblown and silly.

One thing I noticed though, was the output of your code:

TTT = 0 3 TTT = 6 9 TTT = 9 12 DDD = 3 6 TD = 2 4 TTTTTT = 6 12
It shows the TTT bit matching to 6-9 and 9-12. Ideally, I would need that to match 6-9, 7-10, 8-11, and 9-12. No biggie though. Let me play around with it a bit, and see how it works.

Thanks
Matt

Replies are listed 'Best First'.
Re^3: s/// only replacing most of the time...
by suaveant (Parson) on May 29, 2007 at 17:05 UTC
    A little bit of smoke and mirrors, then...
    $_ = 'TTTDDDTTTTTTX'; my @patterns = qw(TTT DDD TD TTTTTT); my %tags; for my $pat (@patterns) { my $pat2 = substr($pat,0,1).'(?='.substr($pat,1,length($pat)).')'; + # T(?=TT) only REALLY matches the first char, but that still works while(/($pat2)/g) { print "$pat = $-[0] $+[0]\n"; push @{$tags{$-[0]}}, "<SPAN CLASS=$pat>"; push @{$tags{$-[0]+length($pat)}}, "</SPAN>"; # instead of usi +ng $+[0] we use $-[0]+the length of the string matched, since it is a + fixed string, no problems. } } my $currentpos = 0; print "$_\n"; for my $pos (sort { $a <=> $b } keys %tags) { print substr($_,0,($pos-$currentpos),''); print join('',@{$tags{$pos}}); $currentpos = $pos; } print $_,"\n";

                    - Ant
                    - Some of my best work - (1 2 3)