The ORA Programming Perl book discusses progressive matching, but only uses m// matching, leaving the question of whether s/// works, too. One would assume so, but if you use the sample code and modify it to use s/// substitutions, none of the examples work anymore.

I also tried using combinations of uses that start with m// in the "while" loop, but using s/// inside the block to modify the string, and then reset the regex position using pos, but again, nothing works. here's sample code:

my $string = "abc abc abc abc"; my $x = 1; while ($string =~ /abc /ig) { my $i = pos($string); $string =~ s/(abc) /$1 def($x)/; print "$string\n"; pos($string) = $i + 6; } print "\nDone. string = '$string'\n";

The desired result should look like this:

abc def(1) abc def(2) abc def(3) abc def(4)

Instead, it produces "abc def(3)def(2)def(1)abc abc abc" and the reason is obvious: the regex always starts back at 0, and it does this no matter what I do. (The above sample code is but one of a variety of attempts.)

In addition to someone solving the specific problem with the code, I'd like a more specific explanation for what the perl rules are concerning progressive matching and the use of substitutions. Is it different than m//? Is it undefined? Is there a specific method documented that I'm not seeing? And why doesn't the above pos call set the position accordingly? UPDATE: Incidentally, I know I can get what I want using this code:

while ($string =~ s/(abc) ([^d])/$1 def($x) $2/i) ...

The point is to understand the proper way of doing progressive matching with substitutions... my so-called 'workaround' won't work in the actual program I'm using, as I can't be sure what the next letter(pattern) would be after the 'abc' match.


In reply to Progressive matching w/substitutions by argv

Title:
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, details, 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, summary, 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.