使用regex只允许字符串中有两个数字

时间:2022-07-26 19:29:02

I need regex that only allows a maximum of 2 digits (or whatever the desired limit is actually) to be entered into an input field.

我需要regex,它只允许在输入字段中输入最多为2位(或任何实际需要的限制)的数字。

The requirements for the field are as follows:

该领域的要求如下:

  1. Allow a-z A-Z
  2. 允许所有所有
  3. Allow 0-9
  4. 允许0 - 9
  5. Allow - and . characters
  6. 允许,。字符
  7. Allow spaces (\s)
  8. 允许空格(\ s)
  9. Do not allow more than 2 digits
  10. 不允许超过两位数
  11. Do not allow any other special characters
  12. 不允许任何其他特殊字符

I have managed to put together the following regex based on several answers on SO:

我已经根据以下几个答案整理了下面的regex:

^(?:([a-zA-z\d\s\.\-])(?!([a-zA-Z]*\d.*){3}))*$  

The above regex is really close. It works successfully for the following:

上面的regex非常接近。它成功的工作如下:

  1. test 12 test
  2. 测试12个测试
  3. test12
  4. test12
  5. test-test.12
  6. test-test.12

But it allows an input of:

但它允许输入:

123 (but not 1234, so it's close).

123(但不是1234,所以接近了)。

It only needs to allow an input of 12 when only digits are entered into the field.

它只需要在只输入数字时允许输入12。

I would like some help in finding a more efficient and cleaner (if possible) solution than my current regex - but it must still be regex, no JS.

我希望得到一些帮助,以找到比我当前的regex更高效、更干净(如果可能的话)的解决方案——但必须是regex,而不是JS。

2 个解决方案

#1


1  

You may use a negative lookahead anchored at the start that will make the match fail once there are 3 digits found anywhere in the string:

你可以在开始时使用一个负面的前视,一旦在字符串中找到3位数字,就会导致匹配失败:

^(?!(?:[^0-9]*[0-9]){3})[a-zA-Z0-9\s.-]*$
 ^^^^^^^^^^^^^^^^^^^^^^^

See the regex demo

看到regex演示

Details:

细节:

  • ^ - start of string
  • ^ -字符串的开始
  • (?!(?:[^0-9]*[0-9]){3}) - the negative lookahead failing the match if exactly 3 following sequences are found:
    • [^0-9]* - zero or more chars other than digits
    • [^ 0 - 9]*——零个或多个数字以外的字符
    • [0-9] - a digit (thus, the digits do not have to be adjoining)
    • [0-9] -一个数字(因此,数字不必相邻)
  • (? !(?:[^ 0 - 9]*[0 - 9]{ 3 })——消极的超前失败比赛如果完全3以下序列发现:[^ 0 - 9]*——零个或多个字符以外的数字[0 - 9]——一个数字(因此,数字不需要的)
  • [a-zA-Z0-9\s.-]* - 0+ ASCII letters, digits, whitespace, . or - symbols
  • [a-zA-Z0-9 \ s。-]* - 0+ ASCII字符、数字、空格。或者——符号
  • $ - end of string.
  • $末端的字符串。

#2


2  

You could use a positive lookahead like

你可以使用积极的前瞻性

(?=^(?:\D*\d\D*){2}$) # only two digits
^[- .\w]+$            # allowed characters

See a demo on regex101.com.

请参阅regex101.com上的演示。

#1


1  

You may use a negative lookahead anchored at the start that will make the match fail once there are 3 digits found anywhere in the string:

你可以在开始时使用一个负面的前视,一旦在字符串中找到3位数字,就会导致匹配失败:

^(?!(?:[^0-9]*[0-9]){3})[a-zA-Z0-9\s.-]*$
 ^^^^^^^^^^^^^^^^^^^^^^^

See the regex demo

看到regex演示

Details:

细节:

  • ^ - start of string
  • ^ -字符串的开始
  • (?!(?:[^0-9]*[0-9]){3}) - the negative lookahead failing the match if exactly 3 following sequences are found:
    • [^0-9]* - zero or more chars other than digits
    • [^ 0 - 9]*——零个或多个数字以外的字符
    • [0-9] - a digit (thus, the digits do not have to be adjoining)
    • [0-9] -一个数字(因此,数字不必相邻)
  • (? !(?:[^ 0 - 9]*[0 - 9]{ 3 })——消极的超前失败比赛如果完全3以下序列发现:[^ 0 - 9]*——零个或多个字符以外的数字[0 - 9]——一个数字(因此,数字不需要的)
  • [a-zA-Z0-9\s.-]* - 0+ ASCII letters, digits, whitespace, . or - symbols
  • [a-zA-Z0-9 \ s。-]* - 0+ ASCII字符、数字、空格。或者——符号
  • $ - end of string.
  • $末端的字符串。

#2


2  

You could use a positive lookahead like

你可以使用积极的前瞻性

(?=^(?:\D*\d\D*){2}$) # only two digits
^[- .\w]+$            # allowed characters

See a demo on regex101.com.

请参阅regex101.com上的演示。