我需要编写一个完整填充以下条件的正则表达式:
我正在尝试编写这个正则表达式,但失败了。 请注意,我可以单独为该条件编写正则表达式,但我需要一个组合的正则表达式。 Plz尽快帮助我。
我得到的解决方案只能填满我的第一个&第二个标准:
string re = @"(?x)
^
# fail if...
(?!
# repeating numbers
0{3,}|1{3,}|2{3,}|3{3,}|4{3,}|5{3,}|6{3,}|7{3,}|8{3,}|9{3,}$
|
# sequential ascending
(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){5} \d $
|
# sequential descending
(?:0(?=9)|1(?=0)|2(?=1)|3(?=2)|4(?=3)|5(?=4)|6(?=5)|7(?=6)|8(?=7)|9(?=8)){5} \d $
)
# match any other combinations of 6 digits
\d{6}
$
";
但是,我需要完整的解决方案(一个合并的正则表达式)来完全满足我的3个标准。
答案 0 :(得分:1)
您可以尝试将第一个条件更改为:
\d*(\d)(?:\d*\1){2,}\d*$
(\d)(?:\d*\1){2,}
将匹配超过2个重复数字(连续或不连续),\d*
允许重复部分之前或之后的任意位数。
意味着您的新正则表达式变为:
string re = @"(?x)
^
# fail if...
(?!
# repeating numbers
\d*(\d)(?:\d*\1){2,}\d*$
|
# sequential ascending
(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){5} \d $
|
# sequential descending
(?:0(?=9)|1(?=0)|2(?=1)|3(?=2)|4(?=3)|5(?=4)|6(?=5)|7(?=6)|8(?=7)|9(?=8)){5} \d $
)
# match any other combinations of 6 digits
\d{6}
$
";
答案 1 :(得分:0)
尝试使用该程序regex coach。在将其插入C#程序之前,您可以更好,更快地分析正则表达式并使其正常工作。
如果你不喜欢这个长正则表达式,你总是可以制作3个正则表达式对象和/或它们。