Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Quick Regex Question

by Anonymous Monk
on Oct 08, 2012 at 14:15 UTC ( #997822=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for a regex that will match the 3 letters INC followed by exactly 7 numbers within a longer string. Here is what I have but it isn't working.

Can anyone give me a quick hand? Thank you.
my $line="asdf asdf asdf INC1234567 klasdv asf asdf "; if ($line =~ /INC[0-9]\{7\}/ ) { print "yes\n"; }else{ print "no\n"; }

Comment on Quick Regex Question
Download Code
Replies are listed 'Best First'.
Re: Quick Regex Question
by toolic (Bishop) on Oct 08, 2012 at 15:00 UTC
    You already have your answer, but here is a way to solve problems like this in the future.

    Tip #9 from the Basic debugging checklist YAPE::Regex::Explain:

    The regular expression: (?-imsx:INC[0-9]\{7\}) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- INC 'INC' ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- \{ '{' ---------------------------------------------------------------------- 7 '7' ---------------------------------------------------------------------- \} '}' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
Re: Quick Regex Question
by Athanasius (Canon) on Oct 08, 2012 at 14:24 UTC
    0:20 >perl -E "my $line = qq[asdf asdf asdf INC1234567 klasdv asf asd +f ]; if ($line =~ /INC\d{7}/ ) { say qq[yes]; } else { say qq[no]; }" yes

    Your only mistake was in backslashing the curly brackets.

    Update: Fixed typo: the second occurrence of $line was missing the sigil.

    Athanasius <°(((><contra mundum

      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?

        OK, change the regex to:


        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:


        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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997822]
Approved by Athanasius
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2016-02-12 07:56 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (391 votes), past polls