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

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.

  • Comment on Re^6: how to remove a string from end of a line

Replies are listed 'Best First'.
Re^7: how to remove a string from end of a line
by AnomalousMonk (Archbishop) on Oct 12, 2015 at 06:53 UTC

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

      I started reading "perlretut". But I struck "?:" here. I cant understand that explanation. In the document I see this example.

      $x = '12aba34ba5'; @num = split /(a|b)+/, $x; # @num = ('12','a','34','a','5') print @num,$/; @num = split /(?:a|b)+/, $x; # @num = ('12','34','5') print @num,$/;

      Based on the regex explained earlier in the document I can write that code like this.

      @num = split /[ab]+/, $x; # @num = ('12','34','5') print @num,$/;

      But I want to know how that "?:" working in the regex. Thanks for reply

        (?:...) works like (...), but doesn't create a capture group (e.g. $1). split returns separators if they're captured, so if you need grouping but don't need the separators, (?:...) becomes handy.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        I agree that  /(a|b)+/ is better written as  /[ab]+/ in the general case. But  /(a|b)+/ was only intended to exemplify the difference between capturing and non-capturing groups in the split built-in function. Note that a and b in the  (a|b) expression could represent any regex expression, not just the literal characters 'a' and 'b'. That's not true in a character class, which can only be composed of literal characters or another character class, e.g., \s or \w. See perlrecharclass.

        ... how that "?:" working in the regex.

        The  (?symbol(s) ...) syntax was introduced with Perl version 5.10 to support a multitude of regular expression extensions. The  (? sequence was never valid in regexes prior to 5.10, so it was a convenient vehicle for these extensions. So you have
            (?:  non-capturing group)
            (?>  atomic group)
            (?=  positive look-ahead)
            (?<! negative look-behind)
            etc.
        See Extended Patterns in perlre. See perlre and perlretut for info on the differences between capturing and non-capturing groups.

        See also Special Backtracking Control Verbs for a similar syntactic twist:  (* was never valid pre-5.10.


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