$str =~ s/(.*)\|.*/$1/;
Another way to look at this simple (i.e., up to Perl version 5.6, inclusive) match:
See YAPE::Regex::Explain. (Note: This module is good only for version 5.6 and earlier regexes.) See also perlre, perlretut, and perlrequick.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 ----------------------------------------------------------------------
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: <%-{-{-{-<
In reply to Re^3: how to remove a string from end of a line
by AnomalousMonk
in thread how to remove a string from end of a line
by rpinnam
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |