Hello Monks
My users create strings which contain text like update 8923 mark complete. I'd like to allow them to create strings like update 8435 and 9323 mark complete and convert those into multiple strings that look like the old pattern, i.e. update 8435 mark complete update 9323 mark complete. The following snippet does just what I want, but the Camel Book, in describing the \G assertion, says

Whenever you start thinking in terms of the pos function, it's tempting to start carving your string up with substr, but this is rarely the right thing to do.

So, should I consider doing something else? Thanks.

#!/usr/bin/perl -w use strict; my $data_stg = 'junk text update 8923 mark complete update 8324 mark ' . 'complete more junk update 5438 and 5843 and 1522 mark ' . 'complete update 8435 and 9323 mark complete true junk'; pos( $data_stg ) = 0; my %mult_updates; my $pass = 1; while ( $data_stg =~ /(update \d+( and \d+)+ mark complete)/ig ) { my $mult_update_pos = pos( $data_stg ); print "$pass pos: '$mult_update_pos'\n"; my $mult_update = $1; print "$pass orig_mult_update: '$mult_update'\n"; my $mult_update_length = length $mult_update; print "$pass length: '$mult_update_length'\n"; $mult_update =~ s/and (\d+)/mark complete update $1/gi; print "$pass new_mult_update: '$mult_update'\n"; $mult_updates{ $mult_update_pos - $mult_update_length } = [ ($mult_update_length, $mult_update) ]; } continue { $pass++; } # Work backwards from the end of the string, doing substr # on positions which have been identified as having code to # replace. Let the key define a starting position and the # key's value contain an array ref describing the length # of the target and the desired replacement text. foreach ( sort {$b <=> $a} keys %mult_updates ) { substr( $data_stg, $_, $mult_updates{$_}->[0], $mult_updates{$_}->[1] ); } print "\n$data_stg\n";

In reply to regex, pos, \G, and substr by ff

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.