小编Cor*_*ong的帖子

正则表达式匹配两组重复数字,其中两个都不允许是相同的数字

伙计们,我正在尝试使用正则表达式处理大量的数字字符串,并匹配特定模式的数字序列,其中某些数字在组中重复.部分要求是确保给定模式的各部分之间的唯一性.

我想要实现的那种匹配的一个例子

ABBBCCDD 
Run Code Online (Sandbox Code Playgroud)

将其解释为一组数字.但A,B,C,D不能相同.每个重复都是我们想要匹配的模式.

我一直在使用带有负面预测的正则表达式作为这种匹配的一部分,它可以工作,但不是所有的时间,我很困惑为什么.我希望有人可以解释为什么会出现故障并提出解决方案.

因此,为了解决ABBBCCDD,我使用负面预测使用组来提出这个RE.

(.)(?!\1{1,7})(.)\2{2}(?!\2{1,4})(.)\3{1}(?!\3{1,2})(.)\4{1}
Run Code Online (Sandbox Code Playgroud)

打破这个..

(.)           single character wildcard group 1 (A)
(?!\1{1,7})   negative look-ahead for 1-7 occurrences of group 1 (A)
(.)           single character wildcard group 2 (B)
\2{2}         A further two occurrences of group 2 (B)
(?!\2{1,4})   Negative look-ahead of 1-4 occurrences of group 2 (B)
(.)           single character wildcard group 3 (C)
\3{1}         One more occurrence of group 3 (C)
(?!\3{1,2})   Negative look-ahead of 1-2 occurrences of group 3 (C)
(.)           single character wildcard …
Run Code Online (Sandbox Code Playgroud)

regex negative-lookahead

7
推荐指数
1
解决办法
853
查看次数

标签 统计

negative-lookahead ×1

regex ×1