Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^9: how to remove a string from end of a line

by AnomalousMonk (Archbishop)
on Oct 12, 2015 at 19:37 UTC ( [id://1144577]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^10: how to remove a string from end of a line
by ravi45722 (Pilgrim) on Oct 13, 2015 at 05:15 UTC

    Now its getting very clear. The example you wrote to remove last 4 is   $s =~ s{ (?: [|] [^|]*){4} \z }{}xms; and can be written also as   $s =~ s{ ( [|] [^|]*){4} \z }{}xms; But if we don't need $1 values we can use "?:". As you advised I read "perlretut". From that I understand the "\z" used to indicate the end of the line and "ms" used for detecting multiple lines and "\n". And the "x" is used for increasing the readability of code in regex using spaces

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1144577]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found