用于验证最大长度为10个字符的整数

时间:2022-05-05 21:37:53

Could you please point me the appropriate RegEx for validating an integer with a maximum length of 10 characters?

您能给我指出一个适当的RegEx来验证一个最大长度为10个字符的整数吗?

Valid ones include: 1234567890

有效的包括:1234567890

5 个解决方案

#1


19  

[^0-9][+-]?[0-9]{1,10}[^0-9]

In words: Optional + or - followed by a digit, repeated one up to ten times. Note that most libraries have a shortcut for a digit: \d, hence the above could also be written as: \d{1,10}.

单词:可选的+或-后面跟着一个数字,重复一个数字十次。注意,大多数库都有一个针对数字的快捷方式:\d,因此上面的代码也可以写成:\d{1,10}。

#2


27  

Don't forget that integers can be negative:

不要忘记整数可以是负数:

^\s*-?[0-9]{1,10}\s*$

Here's the meaning of each part:

以下是每个部分的含义:

  • ^: Match must start at beginning of string
  • ^:比赛必须从字符串的开始
  • \s: Any whitespace character
    • *: Occurring zero or more times
    • *:零次或多次发生
  • \s:任何空格字符*:出现0次或更多次
  • -: The hyphen-minus character, used to denote a negative integer
    • ?: May or may not occur
    • ?:可能发生也可能不发生
  • -:连字符,用于表示负整数?:可能发生也可能不发生
  • [0-9]: Any character whose ASCII code (or Unicode code point) is between '0' and '9'
    • {1,10}: Occurring at least one, but not more than ten times
    • {1,10}:发生至少1次,但不超过10次。
  • [0-9]: ASCII码(或Unicode码点)在'0'和'9'{1,10}之间的任何字符:至少出现一个,但不超过10次
  • \s: Any whitespace character
    • *: Occurring zero or more times
    • *:零次或多次发生
  • \s:任何空格字符*:出现0次或更多次
  • $: Match must end at end of string
  • $:匹配必须在字符串末尾结束

This ignores leading and trailing whitespace and would be more complex if you consider commas acceptable or if you need to count the minus sign as one of the ten allowed characters.

如果您认为逗号是可接受的,或者您需要将负号算作10个允许字符中的一个,那么这就忽略了前导空格和后导空格,并且会更加复杂。

#3


3  

1 to 10:

1到10:

[0-9]{1,10}

In .NET (and not only, see the comment below) also valid (with a stipulation) this:

在。net中(不仅如此,见下面的评论)同样有效(有规定):

\d{1,10}

C#:

c#:

var regex = new Regex("^[0-9]{1,10}$", RegexOptions.Compiled);
regex.IsMatch("1"); // true
regex.IsMatch("12"); // true
..
regex.IsMatch("1234567890"); // true
regex.IsMatch(""); // false
regex.IsMatch(" "); // true
regex.IsMatch("a"); // false

P.S. Here's a very useful sandbox.

这里有一个非常有用的沙箱。

#4


2  

In most languages i am aware of, the actual regex for validating should be ^[0-9]{1,10}$; otherwise the matcher will also return positive matches if the to be validated number is part of a longer string.

在大多数语言中,我意识到,实际的正则表达式验证应该^[0 - 9]{ 1,10 } $;否则,如果要验证的数字是更长的字符串的一部分,那么matcher也将返回正匹配。

#5


0  

0123456789 is not a valid integer (usually zeros will be stripped)

0123456789不是一个有效的整数(通常会去掉0)

I think something like this regexp would be better to use:

我认为使用类似regexp的东西会更好:

^[1-9]([0-9]*)$

(does not support signed numbers)

(不支持签名号码)

#1


19  

[^0-9][+-]?[0-9]{1,10}[^0-9]

In words: Optional + or - followed by a digit, repeated one up to ten times. Note that most libraries have a shortcut for a digit: \d, hence the above could also be written as: \d{1,10}.

单词:可选的+或-后面跟着一个数字,重复一个数字十次。注意,大多数库都有一个针对数字的快捷方式:\d,因此上面的代码也可以写成:\d{1,10}。

#2


27  

Don't forget that integers can be negative:

不要忘记整数可以是负数:

^\s*-?[0-9]{1,10}\s*$

Here's the meaning of each part:

以下是每个部分的含义:

  • ^: Match must start at beginning of string
  • ^:比赛必须从字符串的开始
  • \s: Any whitespace character
    • *: Occurring zero or more times
    • *:零次或多次发生
  • \s:任何空格字符*:出现0次或更多次
  • -: The hyphen-minus character, used to denote a negative integer
    • ?: May or may not occur
    • ?:可能发生也可能不发生
  • -:连字符,用于表示负整数?:可能发生也可能不发生
  • [0-9]: Any character whose ASCII code (or Unicode code point) is between '0' and '9'
    • {1,10}: Occurring at least one, but not more than ten times
    • {1,10}:发生至少1次,但不超过10次。
  • [0-9]: ASCII码(或Unicode码点)在'0'和'9'{1,10}之间的任何字符:至少出现一个,但不超过10次
  • \s: Any whitespace character
    • *: Occurring zero or more times
    • *:零次或多次发生
  • \s:任何空格字符*:出现0次或更多次
  • $: Match must end at end of string
  • $:匹配必须在字符串末尾结束

This ignores leading and trailing whitespace and would be more complex if you consider commas acceptable or if you need to count the minus sign as one of the ten allowed characters.

如果您认为逗号是可接受的,或者您需要将负号算作10个允许字符中的一个,那么这就忽略了前导空格和后导空格,并且会更加复杂。

#3


3  

1 to 10:

1到10:

[0-9]{1,10}

In .NET (and not only, see the comment below) also valid (with a stipulation) this:

在。net中(不仅如此,见下面的评论)同样有效(有规定):

\d{1,10}

C#:

c#:

var regex = new Regex("^[0-9]{1,10}$", RegexOptions.Compiled);
regex.IsMatch("1"); // true
regex.IsMatch("12"); // true
..
regex.IsMatch("1234567890"); // true
regex.IsMatch(""); // false
regex.IsMatch(" "); // true
regex.IsMatch("a"); // false

P.S. Here's a very useful sandbox.

这里有一个非常有用的沙箱。

#4


2  

In most languages i am aware of, the actual regex for validating should be ^[0-9]{1,10}$; otherwise the matcher will also return positive matches if the to be validated number is part of a longer string.

在大多数语言中,我意识到,实际的正则表达式验证应该^[0 - 9]{ 1,10 } $;否则,如果要验证的数字是更长的字符串的一部分,那么matcher也将返回正匹配。

#5


0  

0123456789 is not a valid integer (usually zeros will be stripped)

0123456789不是一个有效的整数(通常会去掉0)

I think something like this regexp would be better to use:

我认为使用类似regexp的东西会更好:

^[1-9]([0-9]*)$

(does not support signed numbers)

(不支持签名号码)