.NET 正则表达式允许 3 个重复字符

.NET Regular Expression allow 3 repeated characters

我正在尝试使用以下条件创建 .NET 正则表达式,但没有成功。我所拥有的只是下面的正则表达式。任何帮助将不胜感激!!

  1. 8-15 个字符(字母或数字,不区分大小写)
  2. 最多允许 3 个重复字符或数字
  3. 没有特殊字符或符号

这是我得到的:

^(?=.*[0-9].*)(?=.*[A-Za-z].*)([0-9A-Za-z]{3}){8,15}$

这个正则表达式可以工作

^(?=.{8,15}$)(?!.*?(.){3})[A-Za-z0-9]+$

Regex Demo

正则表达式分解

^ #Start of string
(?=.{8,15}$) #Lookahead to check there are 8 to 15 digits
(?!.*?(.){3}) #Lookahead to determine that there is no character repeating more than thrice
[A-Za-z0-9]+ #Match the characters
$ #End of string

对于unicode支持,您可以使用

^(?=.{8,15}$)(?!.*?(.){3})[\p{L}\p{N}]+$

注意 :- 要匹配一个字符和一位数字,您可以使用

^(?=.{8,15}$)(?=.*[A-Za-z])(?=.*\d)(?!.*?(.){3})[A-Za-z0-9]+$

Regex Demo