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


in reply to No \G for s///g ?

\G ... is only supported for m//g, not s///g

I didn't dig into your regex, but I think \G is supported for s///g;

$ perl -le '$_="abc def"; print; s/\G\w/X/g; print' abc def XXX def
-Blake

Replies are listed 'Best First'.
Re^2: No \G for s///g ? (oops)
by tye (Sage) on Mar 21, 2003 at 21:52 UTC

    Thanks! I thought I'd used that before but perlop having several examples of m/\G.../g and no examples of s/\G...//g and no mention of \G in the s/// section combined to make me think maybe that was the problem (as I was posting -- writing up a problem always brings new solutions to mind, ya know).

    I verified that this works on my version of Perl as well.

    I'd been meaning to post this for a week and finally had a few minutes while waiting for stuff to compile. This response got me to take some time I didn't have to write up some test cases. I must have had an unseen bug in the code when I was testing before because it works fine now.

    Thanks, blakem.

                    - tye

      Try setting $len to 5. You'll see that the 'tight' version fails to wrap correctly under certain circumstances like this

      Wrapping: @[0;7mCoruscate@[0m says this is a test of the line wrapping code Tight code1: @[0;7mCoruscate@[0m says this is a test of the line wrapping code Tight code: @[0;7mCorus cate@[0m says this is a test of the line wrapp ing code

      which suggested to me that the regexes be executed in opposite order

      Wrapping: @[0;7mCoruscate@[0m says this is a test of the line wrapping code Tight code1: @[0;7mCorus cate@[0m says this is a test of the line wrapp ing code Tight code: @[0;7mCorus cate@[0m says this is a test of the line wrapp ing code

      which seems to do the trick. I believe the first regex gets stymied by lines that have to be split many times at a space, as well as being split many times inside a word.


      ---
      demerphq


        Good catch. Thanks.

                        - tye