Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^4: Quick Regex Question

by AnomalousMonk (Monsignor)
on Oct 09, 2012 at 12:36 UTC ( #998001=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Quick Regex Question
in thread Quick Regex Question

... using a zero-width negative look-ahead assertion; or ... using a z +ero-width positive look-ahead assertion together with \D ...

Note that  (?!\d) and  (?=\D) are not complementaryequivalent. The  (?!\d) assertion is satisfied by having any non-digit follow it or by nothing, i.e., by the end of the string, nothing being not-a-digit. The  (?=\D) assertion must be followed by a non-digit character. And similarly with the  (?<!\d) and  (?<=\D) look-behinds.

>perl -wMstrict -le "my $s = 'xyzINC1234567'; ;; print 'negative assertion true' if $s =~ /INC\d{7}(?!\d)/; print 'positive assertion true' if $s =~ /INC\d{7}(?=\D)/; " negative assertion true

In this behavior,  (?!\d) and, in general,  (?!character-or-character-class) and its negative look-behind cousin are similar to (but not exactly the same as) the  \b assertion, which may be true at the beginning or end of a string.


Comment on Re^4: Quick Regex Question
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-08-23 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls