# At POSition
(?= # Lookahead
( # Capture grp 1
[abcd]{4,} # Get 4 or more (greedy) 'a' or 'b' or 'c' or 'd' characters
)
)
(?= # Lookahead, check for 'a' (still at POS)
[bcd]*a # 0 or more [bcd]'s then 'a'
)
(?= # Lookahead, check for 'b' (still at POS)
[acd]*b # 0 or more [acd]'s then 'b'
)
(?= # Lookahead, check for 'c' (still at POS)
[abd]*c # 0 or more [abd]'s then 'c'
)
(?= # Lookahead, check for 'd' (still at POS)
[abc]*d # 0 or more [abc]'s then 'd'
)
\1 # Backref to capt grp 1, consume it
# Passed test, now at POSition + length of capture group 1
如果字符串必须至少包含abcd,但可以包含其他字符串,则可以使用此方法
(?=.*a)(?=.*b)(?=.*c)(?=.*d)
如果它们也只能包含abcd,那么这可能更好
^(?=.*a)(?=.*b)(?=.*c)(?=.*d)[abcd]+$
更新
为了回答您的问题,如果您正在寻找一个浮动版本,这是您想要的:
(?=([abcd]{4,}))(?=[bcd]*a)(?=[acd]*b)(?=[abd]*c)(?=[abc]*d)\1
扩展:
更多
您可以从搜索字符串系统地构造正则表达式。我对python不太了解,下面是一个如何用Perl实现的示例。但是请注意,字符串越长,查找匹配项所需的时间就越长,但这应该相当快。在
^{pr2}$输出
为什么要在这里使用regex?在
相关问题 更多 >
编程相关推荐