The information you provided isn't much to go on. I'm sure you have more than just the one example string to deal with, so the question is: in what ways are all the other strings similar and not similar to the one example you've given?
Any of the following might serve as the strategy that solves not only the one example you gave, but all the other cases:
- Delete all characters after the last "|" character in the string
- Delete all characters from the 10th "|" character to the end of the line
- Delete all characters after the 10th "|" character, but before the 11th one (if there is an 11th one)
- Delete any contiguous string of non-"|" characters that happens to resemble an email address (or contain "@")
The point is that the solution depends on what you know about the input data - and how consistent the input is and what kinds of consistency it has (or doesn't have) - as well as how the output should differ.
I tend to do a lot of "defensive programming" whenever it comes to applying this sort of filter to text data - a lot of data diagnosis (where possible) before writing the code, and enough tests in the code to make sure that the filter does what it's supposed to do, and reports when the input doesn't meet the necessary expectations. | [reply] |
| [reply] |
Have you tried anything yet? I know that you're new here, so I'll point out that you should always post the code that you've tried or at least state what you've researched to try to solve the problem yourself. Often, you won't get a response at all and you'll take some flack by not showing that you've made some sort of effort yourself.
Here is one way to remove what you want removed, assuming that what is being removed will always be in the same position:
use warnings;
use strict;
my $str = 'RcdA|CON|139|||Kan|13|J|J|607|abc@gmail.com';
$str =~ s/(.*)\|.*/$1/;
print "$str\n";
| [reply] [d/l] |
$ 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] |