简体   繁体   English

正则表达式对数字的验证

[英]Regex expression validation for numbers

This regex expression that I have validate the situation below: 我已经验证了以下情况的此正则表达式:

Correct input: 正确输入:

  1. 12345678,12345678,12345678 12345678,12345678,12345678
  2. *space*12345678 , 12345678 , 12345678 , 12345678 * space * 12345678,12345678,12345678,12345678
  3. 12345678,12345678,*space* 12345678,12345678,*空格*
  4. 12345678 12345678
  5. *space*12345678, *空格* 12345678,
  6. 12345678, 12345678,

Result: return true(regex expression is working correctly for situations above.) 结果:返回true(在上述情况下正则表达式可以正常工作。)

wrong input: 输入错误:

  1. 1234567812345678 1234567812345678

Result: return true (should be false) 结果:返回true(应该为false)

For wrong input it should be returning false , but it return true. 对于错误的输入,它应该返回false,但是返回true。 what should i do to validate for the wrong input? 我应该怎么做才能验证输入错误?

   var validate_commas = /^(\s*\d{8}\s*[,]?\s*)*$/;

Thank you 谢谢

See Ambiguity in description for more details on why your question is ambiguous, and how that affects the answers you get. 有关问题为何含糊不清以及如何影响所获得答案的更多详细信息,请参见描述中的歧义

I assume spaces (ASCII 32) are arbitrarily allowed: 我假设可以任意使用空格(ASCII 32):

  • At the start of the string 在字符串的开头
  • At the end of the string 在字符串的末尾
  • Before and after comma. 逗号前后。

I assume that you want to disallow horizontal tabs, new lines, carriage returns and other whitespace characters matched by \\s , and only allow space (ASCII 32) to be freely specified. 我假设您要禁止使用水平制表符,换行符,回车符和其他与\\s匹配的空格字符 ,并且只允许自由指定空格(ASCII 32)。

The BNF grammar with the above assumptions is: 具有以上假设的BNF语法为:

<DIGIT>  ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<NUMBER> ::= <DIGIT> {8}
<SPACE>  ::= " "
<TOKEN>  ::= <SPACE>* <NUMBER> <SPACE>*
<LIST>   ::= <TOKEN> ( "," <TOKEN> )* [ "," <SPACE>* ]

With the grammar above, it is easy to write the regex solution: 通过上面的语法,可以很容易地编写正则表达式解决方案:

var regex = /^ *\d{8} *(?:, *\d{8} *)*(?:, *)?$/;

Ambiguity in description 描述中的歧义

Rather than showing examples of what string you want to match, as suggested, your should specify a grammar for your input. 不像建议那样显示要匹配的字符串的示例,而是应为输入指定语法。 Examples may not cover all the cases, and the answerer usually assumes "don't care" or make up some assumptions for the cases not stated in the examples. 示例可能不会涵盖所有情况,并且答复者通常会假设“不在乎”或对示例中未提及的情况做出一些假设。

Does removing the optional comma help? 删除可选的逗号有帮助吗?

var validate_commas = /^(\s*\d{8}\s*)(,\s*\d{8}\s*)*,?$/;

Without clearer information, this is the best that can be done without making more assumptions... eg will there ever only be one numbers and no commas? 没有更清晰的信息,这是最好的做法,而无需做更多的假设……例如,是否会有一个数字而没有逗号?

This should work 这应该工作

var validate_commas = /^(\s*\d{8}\s*(?:$|,\s*))*$/;

It will take one or more instances of 8 numeric characters followed by the end of the string or another comma. 它需要一个或多个8个数字字符的实例,然后是字符串的末尾或另一个逗号。

Or alternatively if you do not want the expression to match an empty line, change the last quantifier to the one or more character: + 或者,如果您不希望表达式匹配空行,则将最后一个量词更改为一个或多个字符:+

var validate_commas = /^(\s*\d{8}\s*(?:$|,\s*))+$/;

Will return true on the following inputs: 在以下输入中将返回true

12345678,12345678,12345678
*space*12345678 , 12345678 , 12345678 , 12345678
12345678,12345678,*space*
12345678
*space*12345678,
12345678,

Will return false on the following inputs: 在以下输入中将返回false

1234567812345678
sdsdsfsd
12345678, 12345678 12345678

I hope this helps. 我希望这有帮助。

You could try something like this: 您可以尝试这样的事情:

var validate_commas = /^\s*\d{8}(\s*,\s*(?:\d{8})?)*$/;

Much like the other suggestions, except it makes the additional numbers optional, thus allowing for 12345678, etc. 与其他建议非常相似,不同之处在于它使附加数字成为可选数字,从而允许12345678,等。

You mean the comma is not optional between numbers? 您是说数字之间的逗号不是可选的?

Try this: 尝试这个:

var validate_commas = /^(\s*\d{8}\s*)(,\s*\d{8}\s*)*$/;

Incidentally this doesn't accept the empty string which your example did. 顺便说一句,这不接受您的示例所做的空字符串。 Not sure if you really wanted that. 不知道您是否真的想要那样。

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

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