C# 我需要一个验证给定字符串中最少 7 位数字的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1118150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I need a regex that validates for minimum 7 digits in the given string
提问by Shimmy Weitzhandler
I wanna validate a phone number. My condition is that I want mimimum 7 numbers in the given string, ignoring separators, X, parantheses.
我想验证一个电话号码。我的条件是我想要给定字符串中最少 7 个数字,忽略分隔符、X、括号。
Actually I want to achieve this function in regex:
其实我想在正则表达式中实现这个功能:
Func<string, bool> Validate = s => s.ToCharArray().Where(char.IsDigit).Count() >= 7;
Func<string, bool> RegexValidate = s => System.Text.RegularExpressions.Regex.IsMatch(s, @"regex pattern should come here.")
string x = "asda 1234567 sdfasdf";
string y = "asda sdfa 123456 sdfasdf";
bool xx = Validate(x); //true
bool yy = Validate(y); //false
The purpose of my need is I want to include this regex in an asp:RegularExpressionValidator
我需要的目的是我想在 asp:RegularExpressionValidator 中包含这个正则表达式
采纳答案by Alan Moore
Seven or more digits, mixed with any number of any other kind of character? That doesn't seem like a very useful requirement, but here you go:
七位或更多位数字,与任意数量的任何其他类型的字符混合?这似乎不是一个非常有用的要求,但您可以这样做:
^\D*(?:\d\D*){7,}$
回答by rampion
(?:\d.*){7,}
(?:...)
- group the contained pattern into an atomic unit\d
- match a digit.*
match 0 or more of any character{7,}
match 7 or more of the preceeding pattern
(?:...)
- 将包含的模式分组为一个原子单元\d
- 匹配一个数字.*
匹配 0 个或多个任意字符{7,}
匹配 7 个或更多前面的模式
If the only separators you want to ignore are spaces, dashes, parentheses, and the character 'X', then use this instead:
如果您要忽略的唯一分隔符是空格、破折号、括号和字符“X”,请改用它:
(?:\d[- ()X]*){7,}
[...]
creates a character class, matching any one of the contained characters
[...]
创建一个字符类,匹配包含的任何一个字符
The difference being, for example, that the first regex will match "a1b2c3d4e5f6g7h"
, and the second one won't.
区别在于,例如,第一个正则表达式将匹配"a1b2c3d4e5f6g7h"
,而第二个则不会。
As Gregor points out in the comments, the choice of regex depends on what function you're using it with. Some functions expect a regex to match the entire string, in which case you should add an extra .*
in front to match any padding before the 7 digits. Some only expect a regex to match part of a string (which is what I expected in my examples).
正如 Gregor 在评论中指出的那样,正则表达式的选择取决于您使用它的功能。某些函数需要正则表达式来匹配整个字符串,在这种情况下,您应该.*
在前面添加一个额外的内容以匹配 7 位数字之前的任何填充。有些人只希望正则表达式匹配字符串的一部分(这是我在示例中所期望的)。
According to the documentation for IsMatch()
it only "indicates whether the regular expression finds a match in the input string," not requires it to match the entire string, so you shouldn't need to modify my examples for them to work.
根据它的文档,IsMatch()
它仅“指示正则表达式是否在输入字符串中找到匹配项”,而不要求它匹配整个字符串,因此您不需要修改我的示例即可使其工作。
回答by Greg Beech
Why do you want to use regular expressions for this? The first Validate
function you posted which simply counts the number of digits is vastly more comprehensible, and probably faster as well. I'd just ditch the unnecessary ToCharArray
call, collapse the predicate into the Count
function and be done with it:
为什么要为此使用正则表达式?Validate
您发布的第一个函数只是简单地计算数字的数量,它更容易理解,而且速度也可能更快。我只是放弃不必要的ToCharArray
调用,将谓词折叠到Count
函数中并完成它:
s.Count(char.IsDigit) >= 7;
Note that if you only want to accept 'normal' numbers (i.e. 0-9) then you'd want to change the validation function, as IsDigit
matches many different number representations, e.g.
请注意,如果您只想接受“正常”数字(即 0-9),那么您需要更改验证函数,因为它IsDigit
匹配许多不同的数字表示,例如
s.Count(c => c >= '0' && c <= '9') >= 7;