RegEx for distance in metric system

主宰稳场 提交于 2019-12-10 14:10:24

问题


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


回答1:


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

(\d+).?(\d*)\s*(m|cm|km)



回答2:


Try this:

(?:0|[1-9]\d*)\s*(?:da|[yzafpnμmcdhkMGTPEZY])?m



回答3:


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.



来源:https://stackoverflow.com/questions/1483280/regex-for-distance-in-metric-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!