开发者

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

开发者 https://www.devze.com 2023-02-12 05:23 出处:网络
currently I am using: if (Regex.IsMatch(searchValue, @\"^\\d{11}.*\") && searchValue.Length == 11)

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


Use the following expression:

^\d{11}$


I tend to use the Expresso tool for designing regex patterns, especially as they get complex. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消