Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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
Re: Quick Regex Question
by Athanasius (Monsignor) 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:

        /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

Re: Quick Regex Question
by toolic (Chancellor) 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 ----------------------------------------------------------------------

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2014-08-31 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls