venkatperl55 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Frnds, In this below line there are two "Fate" strings. I want to replace the second Fate|(Fate followed by | with null). 06roadking|Fate||||||||Fate||||||Alive|Alive|Alive|Alive|Alive|Alive How to do in perl Thanks in advance.

Replies are listed 'Best First'.
Re: Replacing with null
by CountZero (Bishop) on Feb 20, 2010 at 08:53 UTC
    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

      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'
      Thanks CountZero
Re: Replacing with null
by 7stud (Deacon) on Feb 20, 2010 at 11:26 UTC

    ...and a more general solution:

    use strict; use warnings; my $str = '06roadking|Fate||Fate|||Alive|'; my $search = 'Fate|'; my $match_pos = rindex $str, $search; substr($str, $match_pos, length $search) = 'whatever'; print $str, "\n"; --output:-- 06roadking|Fate||whatever||Alive|
      It is not what the OP asked for.

      He wants to replace the second Fate| substring, whereas your solution replaces the rightmost Fate| substring.

      If there are only two Fate| substrings in the string, it is equivalent; but if there are more than two, then your solution is wrong.

      Thus your solution is actually not more general, but rather more restrictive since it pre-supposes that there will only be two Fate substrings in the string!

      But do not be disappointed, we are all here to learn and using index was a good idea. Have a look at the following small change to your program:

      my $match_pos = index $str, $search, (index $str, $search) + length $s +earch;
      See what it does? I starts looking for $search after the first occurrence of $search.

      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