6

I want a RegEx to match distance values in metric system. This regex should match 12m, 100cm,1km ignoring white space

sebaferreras
  • 44,206
  • 11
  • 116
  • 134
  • @NickLarsen,@Gumbo,@Paul Dixon thanks @Fragsworth .. its just typos as you said and I am sorry. I really appreciate the efforts of those who made answers @Lira .. simple problems to some may be very hard to others who may be experts in different area .. and be sure that I googled it very well and now I ended up learning RegEx .. so I may help other people now. Thanks for all –  Sep 27 '09 at 11:33
  • IIRC: SI requires a whitespace between the value and the unit. – dalle Aug 26 '10 at 11:59

4 Answers4

12

Try this:

(?:0|[1-9]\d*)\s*(?:da|[yzafpnμmcdhkMGTPEZY])?m
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • first thought the `yzafpnμmcdhkMGTPEZY` part was a joke, those are the SI-prefixes! :-) – u0b34a0f6ae Sep 27 '09 at 11:15
  • 1
    And don't forget the additional 'quantifiers' proposed in a 1993 update to the Jargon File (http://catb.org/~esr/jargon/html/Q/quantifiers.html), namely, groucho/grouchi and harpo/harpi. It wasn't suggested how the conflict between G = Giga (1e9) and G = Grouchi (1e30) could be resolved. – pavium Sep 27 '09 at 11:24
7

And to extend Paul's answer to include decimal place values...

(\d+).?(\d*)\s*(m|cm|km)
Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
  • Good point, though I've tried to produce the simplest regex that matches the general pattern of his examples - integer values of centimetres, metres and kilometres. +1 - welcome to stackoverflow :) – Paul Dixon Sep 27 '09 at 10:39
  • 1
    I did the same originally, but when you beat me to the post, I figured I would extend it :), thanks for the welcome. – Nick Larsen Sep 27 '09 at 10:43
5

As you didn't specify exactly what you wanted, I used your examples to derive that you want find an integer value, followed by optional whitespace, followed by a unit specifier of cm, m or km. So - this is the simplest example of that.

/(\d+)\s*(m|cm|km)/

The first parentheses captures the number, then it skips 0-many whitespace chars before capturing your required units in the second set of parentheses.

As you can see in other answers, you can go beyond this to pick up decimal values, and also capture a wider number of SI unit prefixes too.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
0

The following regular expression requires exact digit matching. And up to 4 decimal places.

^(\d+)[.,]?(\d{1,4})$

And optionally with a unit

^(\d+)[.,]?(\d{1,4})\s*(mm|m|cm|km)$

And one space between the unit

^(\d+)[.,]?(\d{1,4})[ ]?(mm|m|cm|km)$

Matching:
123
123,456
134.456

Not matching:
abc
abc,456
123abc
123,abc

Pomian
  • 11
  • 2
  • This allowes only 4 digits behind . or , This allowes e.g. 123.0 m whis is valid in englisch but not in e.g. german localization. This allowes e.g. 123,0 m which is valid in e.g. german but not in english localization. This does not allow multipe . or , e.g 100.000.000 is valid in german or 100,000,00 for english localization. – Starbax Jun 24 '21 at 12:39