简体   繁体   English

模式匹配器有效数字java

[英]Pattern Matcher valid number java

I want to have a number validation.我想要一个数字验证。 Rules are:规则是:

  1. A number can start with + or - or nothing (it is taken as a positive number)一个数字可以以 + 或 - 开头或什么都没有(它被视为正数)
  2. Cannot start with 0不能从 0 开始
  3. Can have a fraction either .也可以有一个分数。 or ,或者 ,
  4. Cannot end with 0不能以 0 结尾

So acceptable numbers are: +123, -123, 123, 1023, 123.03, 123,03.所以可接受的数字是:+123、-123、123、1023、123.03、123,03。 Non acceptable numbers are: 001, 1.000, any letters不可接受的数字是:001、1.000、任何字母

I give you the expression that I ve built so far, on Dan's Tools.我向您展示了迄今为止我在 Dan's Tools 上构建的表达式。 I have managed almost everything, except expressions after the the fraction.除了分数后面的表达式之外,我几乎管理了所有内容。 Every help is acceptable.每一个帮助都是可以接受的。

Expression: (^(\\+|-?)([1-9]+))([0-9]+)(\\.|,?)表达式: (^(\\+|-?)([1-9]+))([0-9]+)(\\.|,?)

Thanks in advance Nikos提前致谢

Except the fractional part that is missing in your pattern, your regex won't match single digit numbers as you quantified [1-9] and [0-9] with + quantifier requiring at least one char.除了您的模式中缺少的小数部分外,您的正则表达式不会匹配一位数,因为您使用+量词量化[1-9][0-9]需要至少一个字符。

You can use您可以使用

^[+-]?[1-9][0-9]*(?:[.,][0-9]*[1-9])?$

See the regex demo and the regex graph :请参阅正则表达式演示正则表达式图

在此处输入图片说明

Details细节

  • ^ - start of string ^ - 字符串的开始
  • [+-]? - an optional + or - - 一个可选的+-
  • [1-9] - a single non-zero digit [1-9] - 单个非零数字
  • [0-9]* - zero or more digits [0-9]* - 零个或多个数字
  • (?:[.,][0-9]*[1-9])? - an optional fractional part: . - 一个可选的小数部分: . or , and then zero or more digits followed with a single non-zero digit,然后是零个或多个数字,后跟一个非零数字
  • $ - end of string. $ - 字符串的结尾。

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

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