in reply to Re: how to remove a string from end of a line
in thread how to remove a string from end of a line

I am very new to perl. I am aware of "s///g". But I dont understand what you did here. Can you explain this

 $str =~ s/(.*)\|.*/$1/;

If I replace $1 with anything(suppose Perl) It's printing only perl. Say about $1 clearly. Thank you

Replies are listed 'Best First'.
Re^3: how to remove a string from end of a line
by AnomalousMonk (Archbishop) on Oct 12, 2015 at 05:06 UTC
    $str =~ s/(.*)\|.*/$1/;

    1. (.*) Matches and captures all characters, including  | (pipe) (but excluding, in this case, newlines), from the beginning of the string up to, but not including, the right-most pipe. The characters captured are stored in the  $1 regex special variable (see perlvar).
    2. \|.* Matches (but does not capture) all characters from (and including) the right-most pipe to the end of the string.
    3. Effectively, the entire string has now been matched.
    4. /$1/ The replacement portion of the substitution: whatever has been matched is then replaced by whatever was captured in the  $1 capture variable.

    Another way to look at this simple (i.e., up to Perl version 5.6, inclusive) match:

    c:\@Work\Perl\monks>perl -wMstrict -le "use YAPE::Regex::Explain; ;; print YAPE::Regex::Explain->new(qr/(.*)\|.*/)->explain; " The regular expression: (?-imsx:(.*)\|.*) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- \| '|' ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
    See YAPE::Regex::Explain. (Note: This module is good only for version 5.6 and earlier regexes.) See also perlre, perlretut, and perlrequick.

    Update: The regex discussed above will remove the right-most pipe character, but your OPed examples suggest you want to keep this character. If this is so, I would recommend the substitution posted by johngg here.


    Give a man a fish:  <%-{-{-{-<

      very thanks for such clear answer. Ya, I understand that regex now. I am grouping all the string expect last element (By using pipe). And the grouped string saved into $1 by using "(".

       $str =~ s/(.*)\|.*\|.*/$1/;

      now it will remove the last two elements. and it continues. Suppose if I have 60 "|" in my string and I want to delete last 22 then how can I use this. I cant write 22 pipes like "\|.*\|.*".

        I cant write 22 pipes like "\|.*\|.*".

        Why not? ;-)

        But try this:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'RcdA|CON|139|||Kan|13|J|J|607|abc@gmail.com'; print qq{'$s'}; ;; $s =~ s{ (?: [|] [^|]*){4} \z }{}xms; print qq{'$s'}; " 'RcdA|CON|139|||Kan|13|J|J|607|abc@gmail.com' 'RcdA|CON|139|||Kan|13'
        I used your original example data and only removed four instances, not 22 (because there weren't that many), but you get the idea.


        Give a man a fish:  <%-{-{-{-<