简体   繁体   English

正则表达式检查字符串是否只包含数字,长度是11个字符/数字?

[英]Regex to check if string only contain digits and is 11 characters/digits long?

currently I am using: 目前我正在使用:

if (Regex.IsMatch(searchValue, @"^\d{11}.*") && searchValue.Length == 11)
   {
     // Do something
   }

How can I alter the regex to also check the length and not to use the && operator? 如何更改正则表达式以检查长度而不使用&&运算符?

Thanks in advance. 提前致谢。

Changing the pattern to 将模式更改为

@"^\d{11}$"

Should do it 应该这样做

使用以下表达式:

^\\d{11}$

I tend to use the Expresso tool for designing regex patterns, especially as they get complex. 我倾向于使用Expresso工具来设计正则表达式模式,特别是当它们变得复杂时。 It shows a breakdown that clearly tells you what each bit of the regex is doing. 它显示了一个明确告诉您正则表达式正在做什么的细分。 Agree with other posters (Marc/Logicnp) for the answer to the OP's problem. 同意其他海报(Marc / Logicnp)对OP问题的回答。

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

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