http://www.perlmonks.org?node_id=825285


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

I have a similar problem but could not get oha's solution to work...

Problem: I have big files in which all appearances of following class-Attributes shall be shortened like so:
Bla class="x1" class="x2" class="x3" Blabla
==> Bla class="x1 x2 x3" Blabla

With the substitution

s/\s*class=\"([^\"]*)\"\s*class=\"([^\"]*)\"\s*/ class="$1 $2" /sg;

the result is: ==> Bla class="x1 x2" class="x3" Blabla

A loop delivers of course the correct result:

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

But because the substitution would need several passes to get the result this is not my prefered way.
I would like to have an option, that the substitution is starting again at the position right before the replacement and not after the replacement.
I have tried several times to use \G in the substitution like this one:

s/\G?class=\"([^\"]*)\"\s*class=\"([^\"]*)\"/class="$1 $2"/sg;

or

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

but to no success.
Any Idea, to manage this substitution with one pass?

PS: in oha's example there is a little error if the string starts with _ as f.e. $_ = q{_FR7234C__A_B_C_Bonzo_Dog_D_B};