in reply to how to remove a string from end of a line

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";

Replies are listed 'Best First'.
Re^2: how to remove a string from end of a line
by johngg (Canon) on Oct 09, 2015 at 20:15 UTC

    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| $

    Cheers,

    JohnGG

Re^2: how to remove a string from end of a line
by ravi45722 (Pilgrim) on Oct 12, 2015 at 04:18 UTC

    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

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

        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 "\|.*\|.*".