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

$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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: how to remove a string from end of a line
by ravi45722 (Pilgrim) on Oct 12, 2015 at 05:33 UTC

    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:  <%-{-{-{-<

        Sorry. I am new to Perl. For learning I am revising all the old questions. I don't know what to say. It's working very well. In the previous one at least I can get that you are working with "s///g". But now its like Greek picture for me. Please give me some hint to understand that. I will refer the manuals whatever you suggest.