Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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;


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};

In reply to Re^3: Can you assign to pos() in a s/foo/bar/g by Jaikov
in thread Can you assign to pos() in a s/foo/bar/g by johngg

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (6)
    As of 2021-04-16 09:44 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found