Rather than using a capture I think it might be simpler, since the OP mentions "end of a line" specifically, to just remove any non-pipe symbols anchored to the end of the string.
$ perl -Mstrict -Mwarnings -E '
my $str = q{RcdA|CON|139|||Kan|13|J|J|607|abc@gmail.com};
$str =~s{[^|]*$}{};
say $str;'
RcdA|CON|139|||Kan|13|J|J|607|
$
| [reply] [d/l] |
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
| [reply] [d/l] |
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: <%-{-{-{-<
| [reply] [d/l] [select] |
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 "\|.*\|.*".
| [reply] [d/l] |