1

I'm trying to learn regular expression, because I can't do without them.

So, this is a list of different dimension patterns (for products to sale) :

  • 40x30x75
  • 46x38x23-27
  • Ø30H30
  • Ø25-18H27

So, what pattern to use to find each kind of dimensions ?

For example, now, I'm using this to find this kind of pattern 40x30x75, but it not works :

if(preg_match("#^[0-9][x][0-9][x][0-9]#", $dimension))
   echo "ok"

Could you help me ?

0

4 Answers 4

1

Try the following regex:

(^[0-9]+x[0-9]+x[0-9]+$)|(^[0-9]+x[0-9]+x[0-9]+-[0-9]+$)|(^Ø[0-9]+H[0-9]+$)|(^Ø[0-9]+-[0-9]+H[0-9]+$)

So:

if (preg_match("/(^[0-9]+x[0-9]+x[0-9]+$)|(^[0-9]+x[0-9]+x[0-9]+-[0-9]+$)|(^Ø[0-9]+H[0-9]+$)|(^Ø[0-9]+-[0-9]+H[0-9]+$)/", $dimension))
   echo "ok";

It probably can be simplified even more, maybe someone would want to have a go at that?

By the way, did you know about a website called RegExr it allows you to test your regular expessions, it has been very useful to me whenever I work with regex's.

2
  • 1
    The usage of + is fine unless they want to find those with the exact format like 40x30x75. Using + rather than {2} would allow 4x300x75555. Either way, I'd use \d rather than [0-9]. Much shorter :)
    – Svish
    Aug 4, 2011 at 10:25
  • Good thinking, I totally forgot about \d Aug 4, 2011 at 10:28
1

Your regex is missing quantifiers, add a + sign behind the character classes in question to singal you're looking for one or more matches:

if(preg_match("#^[0-9]+x[0-9]+x[0-9]+#", $dimension))
   echo "ok"

By default it's looking for one character of the class only. Single characters do not need the character class (albeit it was not wrong). See the x'es in the example above.

2
  • Using + would match for example 4x300x7555 as well.
    – Svish
    Aug 4, 2011 at 10:30
  • Sure, as written, one or more. That's only one of the many quantifiers to choose from.
    – hakre
    Aug 4, 2011 at 15:45
1

Your regex should be:

^[0-9]{2}x[0-9]{2}x[0-9]{2}$

[0-9] means a single character which is between 0 and 9. So, you either need to have two of those, or use a quantifier thing like {2}. Instead of [0-9] you could also use \d, meaning any digit. So, you could for example write:

^\d\dx\d\dx\d\d$

Tip: If you can't do without regular expressions, want to learn it and have an easier life, I can recommend you get RegexBuddy. Bought it for myself when I just got started, and it has helped me a lot.

2
  • Thanks for the website, very interesting. Concerning your regex : ^[0-9]{2}[x][0-9]{2}[x][0-9]{2}, it's not "target" exactly that I want. Indeed, I can get this kind of pattern : 24x10x5à10x14x12
    – bahamut100
    Aug 4, 2011 at 11:32
  • 1
    Wasn't sure if you wanted to find them in larger text or match a string or what, so I just left the anchor you had there already. The ^ means that it should start there. Added a $ meaning the text should end there. This means it should only match 00x00x00 and not 00x00x00x00. Also, you don't need to wrap the x in []. You only need to use those if you want to allow more than one character in that spot.
    – Svish
    Aug 4, 2011 at 12:13
0

This will validate the first two:

^[0-9]+x[0-9]+x[0-9]+-?[0-9]*$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.