Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Quick Regex Question

by Anonymous Monk
on Oct 08, 2012 at 14:31 UTC ( #997824=note: print w/replies, xml ) Need Help??


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

Thank you for the quick reply. I must be doing something wrong as now it matches 7,8,9,10, etc numbers. I am looking to only match exactly 7?

Replies are listed 'Best First'.
Re^3: Quick Regex Question
by Athanasius (Chancellor) on Oct 08, 2012 at 14:37 UTC

    OK, change the regex to:

    /INC\d{7}(?:[^\d]|$)/

    which should match only exactly 7 digits. (INC followed by 7 digits followed by either a non-digit or the end of the string.)

    Update: Better solutions:

    /INC\d{7}(?!\d)/

    using a zero-width negative look-ahead assertion; or:

    /INC\d{7}(?:(?=\D)|$)/ # Corrected: See post by AnomalousMonk, belo +w.

    using a zero-width positive look-ahead assertion together with \D as per GrandFather’s suggestion. See Extended Patterns.

    Athanasius <°(((><contra mundum

      You can use \D for "not a digit". In like fashion you can use \W and \S to complement the common word and space matches.

      True laziness is hard work
      ... 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997824]
help
Chatterbox?
[mz2255]: I'm trying to post a question to "seekers of perl wisdom" but always getting a permission denied when I want to submit. There are no links in my post...
[ambrus]: again? someone complained about this just a few days ago (although eventually they could post)
[choroba]: Can you post the question in mz2255's scratchpad?
[ambrus]: Corion, if you're here, can you check the spam filter logs to see what's triggering this time?
[ambrus]: Petroza had trouble posting yesterday, but has posted Issues Fetching URL with a variable token since.
[mz2255]: Yes, just edited the scratchpad. Not sure if I'm doing something wrong, it's my first time.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2017-10-19 15:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (255 votes). Check out past polls.

    Notices?