views:

46

answers:

2

Im using PHP preg_match() method. I am having trouble with a regular expression to match a mm length. The numeerical length can be a float...here are some exmaples of the data I am working on;

1.0mm

25.5mm

3mm

3 mm

3.3 mm

So bacially any integer or float proceeded by "mm" with or without whitespace between.

Thanks!

+1  A: 

Try '/(\d+(?:\.\d+)?)\s*mm/' and you'll have the number in $1.

Explanation:

  • ( begin capturing group
  • \d+ one or more digits
  • (?: begin non-capturing group
  • \. litteral dot
  • \d+ one or more digits
  • ) end non-capturing group
  • ? the non-capturing group ("." + digits) may not be present
  • ) end capturing group
  • \s* zero or more whitespaces (or tabs, etc.)
  • mm literal "mm"
streetpc
Typo: one too many periods.
FM
@streetpc - nearly there! but this reg-ex also returns boolean true on the following;1.2m(it needs to be strict an return true only on "mm")Can this reg-ex be modified to do this?
The regex contains a litteral double "m" so it should only match "mm". If it doesn't, can you put the piece of code please? @FM fixed that, thanks
streetpc
+1  A: 

Do you need a regex for this? if you use something like floatval on "123.122mm" it should give you 123.122 numeric and ignore the text.

glenatron