.NET的正则表达式连续超过2个字母字母、正则表达式、NET

2023-09-07 09:13:12 作者:Renaitre(复活)

我试图写一个.net正则表达式超过2 consecutives字母。

I am attempting to write a .Net Regex for more than 2 consecutives letters.

aa - fine
Aa - fine
aaa - not allowed
Aaa - not allowed

我是新来的正则表达式,但是这是我所拼凑至今。

I am new to regex, but this is what I have pieced together so far.

if (Regex.IsMatch(Password, @"/[^A-Za-z]{2}/"))
    return "Password cannot contain 3 consecutive same letters"; 

我不知道这是否是关闭与否。

I am not sure if this is close or not.

推荐答案

您需要删除的斜线(他们为什么呢?这是不是PHP),你可以用忽略大小写标志。像:

You need to remove the slashes (why are they there? this is not PHP) and you could use the ignore case flag. Like:

Regex.Match(pw, @"(?i)(.)\1\1")

这是一样的:

Which is same as:

Regex.Match(pw, @"(.)\1\1", RegexOptions.IgnoreCase)

由于评论由伊利亚-G。

As commented by Ilia G.