Jump to content

#@%^!*? Regex!


jenuine

Recommended Posts

I've looked at a number of tutorials and can't figure out what should be a simple regex. I've tried many different ways, and with each change, I'd think "this has to be it", but it never is!! #@$%!

 

Here's a piece of my code:

elseif ($size_type == 'mc' && !preg_match("/[0-9]{1,2}x+[0-9]{1,2}/",$itemsize) && !preg_match("/[0-9]{1}(?:XS|XL)/",$itemsize) && !preg_match("/(?:\bXS\b|\bSmall\b|\bMedium\b|\bLarge\b|\bXL\b|[0-9]{1,2})/",$itemsize))

 

An error message is given if all of these preg_match expressions are false. But it's allowing me to enter "1x" and "123" and "12XL" without error. The first preg_match is supposed to say that there needs to be 1 or 2 digits followed by an "x" followed by 1 or 2 more digits. The second is supposed to allow only 1 digit followed by "XS" or "XL", and the third should allow any of the given words or a 1 or 2 digit number. The curly braces aren't working at all. What is the matter?

 

Also, is there any way to shorten the code and have just a single preg_match?

Link to comment
Share on other sites

Well, finally I got it working!, like this:

 

elseif ($size_type == 'mc' && !preg_match("/^([0-9]{1,2})x([0-9]{1,2})$/", $itemsize) && !preg_match("/^[0-9]{1}(?:XS|XL)$/",$itemsize) && !preg_match("/^(XS|Small|Medium|Large|XL|[0-9]{1,2})$/",$itemsize))

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.