Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^5: Can you assign to pos() in a s/foo/bar/g

by Jaikov (Initiate)
on Feb 25, 2010 at 23:21 UTC ( #825406=note: print w/replies, xml ) Need Help??


in reply to Re^4: Can you assign to pos() in a s/foo/bar/g
in thread Can you assign to pos() in a s/foo/bar/g

Thx John, your tip was very helpful, as it gives me the right idea...

Perhaps I did not explain good enough the scenario. But both your solutions have a little error, example:
"Bla class="x1" class="x2" otheratr="y0" class="x3" Blabla
==> Bla class="x1 x2" otheratr="y0 x3" Blabla
correct would be:
Bla class="x1 x2" otheratr="y0" class="x3" Blabla
(perfect would be:
Bla class="x1 x2 x3" otheratr="y0" Blabla)

one-pass solution:

s/(?<=class=")([^"]*)"\s*class="/$1 /g;

But for smaller files with few iterations the while-loop solution is faster, probably because of the lookbehind operator.

1 while $s1 =~ s/class=\"([^\"]*)\"\s*class=\"([^\"]*)\"/class="$1 $2"/g;

Thx again,
Jaikov

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://825406]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2020-10-22 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (227 votes). Check out past polls.

    Notices?