Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Match only last occurrence (?!.*)

by tye (Sage)
on May 31, 2016 at 14:52 UTC ( [id://1164598]=note: print w/replies, xml ) Need Help??


in reply to Re: Match only last occurrence
in thread Match only last occurrence

(Quoting part of the original code from before a silent update was done. Note that the correction that follows still applies to the 2nd version of the code but, of course, may not apply to some future version of the code if more updates are made.)

/(\d{3})(?!\d{3})$/s

Well, you picked an example where that works but not for the reasons that you think. Your example only works because you picked an example string that matches the much simpler:

/(\d{3})$/s

A regex that is actually functionally identical to your regex.

What you wanted was more like:

/(\d{3})(?!.*\d{3})/s

(Update: Removed the '$' from before the last closing paren.)

But I'd still prefer the ( ... )[-1] approach as complex regexes often lead to mistakes, as we have seen twicethrice already in this thread.

- tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-29 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found