$str =~ s/(.*)\|.*/$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).
-
\|.* Matches (but does not capture) all characters from (and including) the right-most pipe to the end of the string.
-
Effectively, the entire string has now been matched.
-
/$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: <%-{-{-{-<
| [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] |
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: <%-{-{-{-<
| [reply] [d/l] [select] |