Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

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

In the sprit of this, also a generalized approach. No benchmarking done for two regexes used or versus other approaches. Note also that the index of the occurrence of the pattern which will be replaced is now zero-based. (Also: This approach could be generalized yet further by passing either a plain replacement string or a code reference. The string/reference could then be fed as appropriate to one of two  s/// substitutions, one without a /e regex modifier, one with. Code of a replacement reference would have access to all capture variables, etc.) All tests pass.

use warnings; use strict; use Test::More # tests => ?? + 1 # Test::NoWarnings adds 1 test 'no_plan' ; use Test::NoWarnings; my $f1 = qr{ foo | (?i: bar) | baz }xms; # find regex my $r1 = qq{Zonk}; # replacement string for my $ar_vector ( # test vectors # i find replace target expected # regex string string string [ 0, $f1, $r1, '', '', ], [ 1, $f1, $r1, '', '', ], [ 2, $f1, $r1, '', '', ], [ 0, $f1, $r1, 'x', 'x', ], [ 1, $f1, $r1, 'x', 'x', ], [ 2, $f1, $r1, 'x', 'x', ], [ 0, $f1, $r1, 'foo', 'Zonk', ], [ 1, $f1, $r1, 'foo', 'foo', ], [ 2, $f1, $r1, 'foo', 'foo', ], [ 0, $f1, $r1, 'xfoo', 'xZonk', ], [ 0, $f1, $r1, 'foox', 'Zonkx', ], [ 0, $f1, $r1, 'xfoox', 'xZonkx', ], [ 0, $f1, $r1, 'xfoofoox', 'xZonkfoox', ], [ 0, $f1, $r1, 'xfooxfoox', 'xZonkxfoox', ], [ 1, $f1, $r1, 'xfoo', 'xfoo', ], [ 1, $f1, $r1, 'foox', 'foox', ], [ 1, $f1, $r1, 'xfoox', 'xfoox', ], [ 1, $f1, $r1, 'xfoofoox', 'xfooZonkx', ], [ 1, $f1, $r1, 'xfooxfoox', 'xfooxZonkx', ], [ 2, $f1, $r1, 'xfoo', 'xfoo', ], [ 2, $f1, $r1, 'foox', 'foox', ], [ 2, $f1, $r1, 'xfoox', 'xfoox', ], [ 2, $f1, $r1, 'xfoofoox', 'xfoofoox', ], [ 2, $f1, $r1, 'xfooxfoox', 'xfooxfoox', ], [ 0, $f1, $r1, 'BAR', 'Zonk', ], [ 0, $f1, $r1, 'BAR', 'Zonk', ], [ 0, $f1, $r1, 'BARx', 'Zonkx', ], [ 0, $f1, $r1, 'xBARx', 'xZonkx', ], [ 0, $f1, $r1, 'BarbAR', 'ZonkbAR', ], [ 0, $f1, $r1, 'xBarbAR', 'xZonkbAR', ], [ 0, $f1, $r1, 'BarxbAR', 'ZonkxbAR', ], [ 0, $f1, $r1, 'BarbARx', 'ZonkbARx', ], [ 0, $f1, $r1, 'xBarxbARx', 'xZonkxbARx', ], [ 1, $f1, $r1, 'BarbAR', 'BarZonk', ], [ 1, $f1, $r1, 'xBarbAR', 'xBarZonk', ], [ 1, $f1, $r1, 'BarxbAR', 'BarxZonk', ], [ 1, $f1, $r1, 'BarbARx', 'BarZonkx', ], [ 1, $f1, $r1, 'xBarxbARx', 'xBarxZonkx', ], [ 2, $f1, $r1, 'BarbAR', 'BarbAR', ], [ 2, $f1, $r1, 'xBarbAR', 'xBarbAR', ], [ 2, $f1, $r1, 'BarxbAR', 'BarxbAR', ], [ 2, $f1, $r1, 'BarbARx', 'BarbARx', ], [ 2, $f1, $r1, 'xBarxbARx', 'xBarxbARx', ], ) { my ($i, $find, $repl, $target, $expected) = @$ar_vector; my $got = replace_ith($i, $find, $repl, $target); is $got, $expected, qq{'$target' -> '$expected'}; } # end for test_vectors # subroutines ###################################################### sub replace_ith { my ($i, # 0-based index of occurrence of pattern to be repla +ced $find, # find pattern: Regexp object $repl, # replacement string $string, # string to be searched/replaced ) = @_; $string =~ s{ # (?: $find (?: (?! $find) . )*){$i} # ok (?: $find (?: .*? (?= $find))){$i} # ok - maybe faster? \K $find } {$repl}xms; return $string; }

In reply to Re^2: Replace the nth occurence by AnomalousMonk
in thread Replace the nth occurence by Anonymous Monk

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (10)
    As of 2014-09-18 11:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (113 votes), past polls