in reply to Replacing with null

This works on my AS Perl 5.10:
my $string = '06roadking|Fate||||||||Fate||||||Alive|Alive|Alive|Alive +|Alive|Alive'; $string =~ s/Fate.*?\KFate\|/\0/; print $string;
Output:
06roadking|Fate||||||||NULL|||||Alive|Alive|Alive|Alive|Alive|Alive
It comes straight from the docs:
(?<=pattern)  \K
A zero-width positive look-behind assertion. For example, /(?<=\t)\w+/ matches a word that follows a tab, without including the tab in $&. Works only for fixed-width look-behind.

There is a special form of this construct, called \K, which causes the regex engine to "keep" everything it had matched prior to the \K and not include it in $&. This effectively provides variable length look-behind. The use of \K inside of another look-around assertion is allowed, but the behaviour is currently not well defined.

For various reasons \K may be significantly more efficient than the equivalent (?<=...) construct, and it is especially useful in situations where you want to efficiently remove something following something else in a string. For instance

s/(foo)bar/$1/g;
can be rewritten as the much more efficient
s/foo\Kbar//g;

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Replacing with null
by AnomalousMonk (Archbishop) on Feb 20, 2010 at 18:18 UTC

    Here's a more general approach using 5.10's  \K assertion. The  {n} quantifier (where n could be $n) allows any occurrence to be replaced.

    >perl -wMstrict -le "my $s = 'Foo|Fate|||Fate||||Pre|Fate|Post|||||Alive|Alive'; my $Fate = qr{ Fate }xms; my $not_Fate = qr{ (?! $Fate) . }xms; $s =~ s{ (?: $Fate $not_Fate*){2} \K $Fate }{}xms; print $s; " Foo|Fate|||Fate||||Pre||Post|||||Alive|Alive

    Update: Here's a general approach based on split, but I doubt it will be significantly faster (if at all) than the regex approach. Works only for positive values of $n.

    >perl -wMstrict -le "my $s = 'Foo|1ate|||2ate||||Pre|3ate|Post|||||4ate|Alive'; my $Fate = qr{ \d ate }xms; my $n = shift; my $x = 2 * $n; $s = join '', grep defined, (split /($Fate)/, $s, $n + 1)[0 .. $x - 2, $x] ; print qq{'$s'}; " 3 'Foo|1ate|||2ate||||Pre||Post|||||4ate|Alive'
Re^2: Replacing with null
by venkatperl55 (Initiate) on Feb 20, 2010 at 10:28 UTC
    Thanks CountZero