正则表达式至少匹配每个字符

2024-09-27 23:19:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python编写一个解决谜题的应用程序。我正在搜索一些文本以查找字符组合,如果我有一组字符[abcd],则需要在文本中查找只包含字符abcd的子字符串,并且必须至少包含每个字符的一次出现-以便字符abcd匹配dcba或abcdd,而不是acd、bbcd或abced。如果使用regex[abcd]+那么我将得到不包含每个字符的子字符串


Tags: 字符串文本应用程序字符regexabcd谜题acd
2条回答

如果字符串必须至少包含abcd,但可以包含其他字符串,则可以使用此方法

(?=.*a)(?=.*b)(?=.*c)(?=.*d)

如果它们也只能包含abcd,那么这可能更好

^(?=.*a)(?=.*b)(?=.*c)(?=.*d)[abcd]+$

更新

为了回答您的问题,如果您正在寻找一个浮动版本,这是您想要的:

(?=([abcd]{4,}))(?=[bcd]*a)(?=[acd]*b)(?=[abd]*c)(?=[abc]*d)\1

扩展:

      # 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

更多

您可以从搜索字符串系统地构造正则表达式。我对python不太了解,下面是一个如何用Perl实现的示例。但是请注意,字符串越长,查找匹配项所需的时间就越长,但这应该相当快。在

^{pr2}$

输出

Found 'bddaaabcabbad'
Found 'abcd'
Found 'dcba'
Found 'abbcdd'


============
Search string = 'abcd'
Normalized    = 'abcd'
Formed regex  =
(?=([abcd]{4,}))(?=[bcd]*a)(?=[acd]*b)(?=[abd]*c)(?=[abc]*d)\1


============
Search string = '%^&*'
Normalized    = '%^&*'
Formed regex  =
(?=([\%\^\&\*]{4,}))(?=[\^\&\*]*\%)(?=[\%\&\*]*\^)(?=[\%\^\*]*\&)(?=[\%\^\&]*\*)\1


============
Search string = 'hi( )there'
Normalized    = 'hi( )ter'
Formed regex  =
(?=([hi\(\ \)ter]{8,}))(?=[i\(\ \)ter]*h)(?=[h\(\ \)ter]*i)(?=[hi\ \)ter]*\()(?=[hi\(\)ter]*\ )(?=[hi\(\ ter]*\))(?=[hi\(\ \)er]*t)(?=[hi\(\ \)tr]*e)(?=[hi\(\ \)te]*r)\1


============
Search string = '==-yes'
Normalized    = '=-yes'
Formed regex  =
(?=([\=\-yes]{5,}))(?=[\-yes]*\=)(?=[\=yes]*\-)(?=[\=\-es]*y)(?=[\=\-ys]*e)(?=[\=\-ye]*s)\1

为什么要在这里使用regex?在

def hasChars(search_string, chars):
    return all(x in search_string for x in chars)

>>> hasChars('aaabcd', 'abc')
True

相关问题 更多 >

    热门问题