Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Replace the nth occurence

by AnomalousMonk (Abbot)
on Nov 21, 2012 at 22:49 UTC ( #1005022=note: print w/ replies, xml ) Need Help??


in reply to Re: Replace the nth occurence
in thread Replace the nth occurence

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


Comment on Re^2: Replace the nth occurence
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005022]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-09-17 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (91 votes), past polls