Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why Doesn't this regex return a positive result?

Posted on 2014-03-07
10
Medium Priority
?
283 Views
Last Modified: 2014-03-07
I'm not familiar with regex and a temp programmer used this code.
        $pattern = '^[AD]([HF][12]|[Z][1])([0][1234569]|[1][0-9]|[2][123]|[3][34]|[4][0]|[5][1346])([0][12345679]|[1][0])([0][12345]|[1][03]|[2][03679]|[3][2389]|[4][01]|[5][1]|[9][5])([0][0123458]|[1][8]|[2][68]|[3][8]|[4][8]|[H][B])*^';
        
        //check if the product code matches a valid code pattern
        if(!preg_match($pattern, $pc)){
            $error = 'Please enter a valid product code';
        }

Open in new window

$pc is equal to "AF141020100"

I'm wondering if someone could explain how this code does not match the pattern specified.
0
Comment
Question by:dmleves49
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
10 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39913593
Because it expects to be AF140, not AF141

HTH,
Dan
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 39913604
Try this pattern to match:
$pattern='^[AD]([HF][12]|[Z][1])([0][1234569]|[1][0-9]|[2][123]|[3][34]|[4][01]|[5][1346])([0][12345679]|[1][0])([0][12345]|[1][03]|[2][03679]|[3][2389]|[4][01]|[5][1]|[9][5])([0][0123458]|[1][8]|[2][68]|[3][8]|[4][8]|[H][B])*$'

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39913722
http://xkcd.com/1171/

Try writing down the rules for the product code.  I'll bet the regular expression could be a LOT simpler.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:dmleves49
ID: 39913751
Thanks Dan. I was able to aquaint myself with regex enough to figure it out. The first two characters could only equal 33 or 34 if they started with a 3 or 40 if they started with a 4, etc.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39913758
OK. So what was deficient in my answer?
See here for the grading guide: http://support.experts-exchange.com/customer/portal/articles/481419-what-grade-should-i-award-

In case of any other grade than A you are required to explain what was deficient about the solution.

Thank you.
0
 

Author Comment

by:dmleves49
ID: 39913944
I'm new here so I don't want to cause any problems as I appreciated everyone's help. I do however feel that getting the answer correct isn't actually what I requested.

I would have expected something like what I originally discovered. For Example:

The reason that your product code does not match the regex is because each set of parentheses is testing 2 digits. The first set of digits that do not match in your regex are "41" because [4][0] will only match "40". If you used [4][01] then it would match 40 or 41.

I'll upgrade your mark though. I just thought it required a bit of explanation since I said I wasn't familiar with regex.
0
 

Author Closing Comment

by:dmleves49
ID: 39913950
Sorry Dan , I only saw your code comment, not your explanation. My bad.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39914167
No worries, mate. Thanks for the points and welcome to EE.

Dan
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
How many times a day do you open, acknowledge, or close an IT incident? What’s your process? Do you have a process depending on the incident, systems involved, and other factors? New Relic Alerts gives you options for how you interact with notifica…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question