简体   繁体   English

正则表达式内部数字检查

[英]Regular Expression inner Digits check

I have the following regex but it fails (the inner digits with the points) : 我有以下正则表达式,但它失败(带有点的内部数字):

([0-9]{1,3}\.?[0-9]{1,3}\.?[0-9]{1,3})

I want that it covers the following cases: 我希望它涵盖以下情况:

  • 123 valid 123有效
  • 123.4 valid 123.4有效
  • 123.44 valid 123.44有效
  • 123.445 valid 123.445有效
  • 123. 33 .3 not ok (regex validates it as true) 123. 33 .3不行(正则表达式验证为真)
  • 123. 3 .3 not ok (regex validates it as true) 123 3.3不可接受(正则表达式验证它为真)
  • 123.333.3 valid 123.333.3有效
  • 123.333.34 valid 123.333.34有效
  • 123.333.344 valid 123.333.344有效

Can you please help me? 你能帮我么?

You have multiple case, I would like to use | 你有多个案例,我想用| the or operator like this : 像这样的运算符:

^([0-9]{1,3}|[0-9]{1,3}\.[0-9]{1,3}|[0-9]{1,3}\.[0-9]{3}\.[0-9]{1,3})$
^           ^                      ^                                 ^

you can check the regex demo 你可以查看正则表达式演示


details 细节

The regex match three cases : 正则表达式匹配三种情况:

case 1 情况1

[0-9]{1,3}

this will match one or more digit 这将匹配一个或多个数字

case 2 案例2

[0-9]{1,3}\.[0-9]{1,3}

this will match one or more digit followed by a dot then one or more digits 这将匹配一个或多个数字,后跟一个点,然后是一个或多个数字

case 3 案例3

[0-9]{1,3}\.[0-9]{3}\.[0-9]{1,3}

this will match one or more digit followed by a dot then three digits then a dot then one or three digits 这将匹配一个或多个数字后跟一个点然后三个数字然后一个点然后一个或三个数字

Note you can replace [0-9] with just \\d your regex can be : 注意你可以用\\d替换[0-9]你的正则表达式可以是:

^(\d{1,3}|\d{1,3}\.\d{1,3}|\d{1,3}\.\d{3}\.\d{1,3})$

How about this one (demo at Regex101 ). 这个怎么样(在Regex101演示)。 It's pretty short and straightforward Regex: 这是非常简短直接的正则表达式:

(^\d{3}\.\d{3}\.\d{1,3}$)|(^\d{3}\.\d{1,3}$)|(^\d{3}$)

This recognizes three valid separate groups. 这可识别三个有效的单独组。

  • (^\\d{3}\\.\\d{3}\\.\\d{1,3}$) as a group which must have 3 digits, a dot, 3 more digits, a dot and 1-3 digits. (^\\d{3}\\.\\d{3}\\.\\d{1,3}$)作为一个必须有3位数字,一个点,3个数字,一个点和1-3位数的组。
  • (^\\d{3}\\.\\d{1,3}$) as a group which must have 3 digits, a dot and 1-3 digits. (^\\d{3}\\.\\d{1,3}$)作为一个必须有3位数,一个点和1-3位数的组。
  • (^\\d{3}$) as a group which must have 1-3 digits. (^\\d{3}$)作为必须具有1-3位数的组。

These groups split with the or ( | ) statement. 这些组用or| )语句拆分。

However, since you have tagged , why don't let Java to take some responsibility and help Regex where isn't strong? 但是,既然你已经标记了 ,为什么不让Java承担一些责任并帮助Regex在哪里不强? I would rather match the format ((?:\\d{1,3}\\.?)+) and check programmatically whether the count of numbers is valid. 我宁愿匹配格式((?:\\d{1,3}\\.?)+)并以编程方式检查数字计数是否有效。

Use the following expression with .matches : 将以下表达式与.matches一起.matches

s.matches("\\d{1,3}(?:\\.\\d{3})?(?:\\.\\d{1,3})?")

See the regex demo 请参阅正则表达式演示

Details 细节

  • ^ - implicit, not necessary as the pattern is used in .matches that requires a full string match ^ - 隐式,不必要,因为模式用于需要完整字符串匹配的.matches
  • \\d{1,3} - 1 to 3 digits \\d{1,3} - 1到3位数
  • (?:\\.\\d{3})? - an optional . - 可选的. and 3 digits 和3位数
  • (?:\\.\\d{1,3})? - an optional sequence of . - 可选的序列. and 1 to 3 digits 和1到3位数
  • $ - implicit, not necessary since the pattern is used in .matches that requires a full string match $ - 隐式,不必要,因为模式用于需要完整字符串匹配的.matches

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM