in reply to Re^3: how to remove a string from end of a line
in thread how to remove a string from end of a line

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

Replies are listed 'Best First'.
Re^5: how to remove a string from end of a line
by AnomalousMonk (Archbishop) on Oct 12, 2015 at 05:48 UTC
    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:  <%-{-{-{-<

      Sorry. I am new to Perl. For learning I am revising all the old questions. I don't know what to say. It's working very well. In the previous one at least I can get that you are working with "s///g". But now its like Greek picture for me. Please give me some hint to understand that. I will refer the manuals whatever you suggest.

        Well, here's a hint to get you started:

        c:\@Work\Perl\monks>perl -wMstrict -le "use YAPE::Regex::Explain; ;; print YAPE::Regex::Explain->new(qr{ (?: [|] [^|]*){4} \z }xms)->expla +in; " The regular expression: (?msx-i: (?: [|] [^|]*){4} \z ) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?msx-i: group, but do not capture (with ^ and $ matching start and end of line) (with . matching \n) (disregarding whitespace and comments) (case-sensitive): ---------------------------------------------------------------------- (?: group, but do not capture (4 times): ---------------------------------------------------------------------- [|] any character of: '|' ---------------------------------------------------------------------- [^|]* any character except: '|' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ){4} end of grouping ---------------------------------------------------------------------- \z the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
        But it's difficult to answer questions you haven't asked. Again, please see perlre, perlretut, and perlrequick. (In particular, perlretut has a lot of info. And all this on-line documentation is available at your local command line via  perldocperlretut   and etc.) Also, please see the articles in the Pattern Matching, Regular Expressions, and Parsing section of the Monastery Tutorials; some of them may address your questions. All this stuff is free, but there are some good book$ out there if you want some recommendations. davido has a nice regex exerciser; see his personal node for a link. I and others will be very happy to answer your questions, but please try to make them as specific as possible.


        Give a man a fish:  <%-{-{-{-<