Regex for positive float numbers

后端 未结 4 780
小蘑菇
小蘑菇 2020-11-30 08:56

For example:
10
0.1
1.23234
123.123
0.000001
1.000
.3

And wrong examples:
0001.2
-12
-1.01<

4条回答
  •  伪装坚强ぢ
    2020-11-30 09:26

    I've stumbled on this page a few times, here is my solution for any one who stumbles here after me:

    A regex like a=(\d+\.?\d* | \d*\.?\d+) matches all decimals numbers without a sign but includes things like 002.0

    A regex to filter those things are b=[1-9\.]+.*

    So one solution is to say it matches the criteria if a & b matches. Or equivalently (contrapositive), see if there is no match for !a | !b. Unfortunately, most languages don't have a complete regex package; the 'and' and negate functions of regular languages isn't present usually. Two simple regexes I've found in code looks a lot nicer and are more maintainable than one complex one (I say this in context to this question & similar situations)

提交回复
热议问题