1

假设我在内存中有一个字符串列表(基本上是 String1 - String100)

String1
String2
...
String11
String12
...
String20
String21
...

使用正则表达式可以执行以下操作的一种方法是什么?

'匹配所有不包含“String1”但包含“String10”或“String3”的字符串'

4

2 回答 2

2

您可以为此使用前瞻断言:

^(?=.*String(?:10|3)\b)(?!.*String1\b)

如果字符串包含String10or String3,这将匹配,但前提是它不包含String1(假设这些单词以某种方式分隔,例如由空格或其他非字母数字字符)。

匹配本身的长度为零,因此您只需要检查是否存在匹配:

>>> strings = ["String10 String1 String5", "String4", "String10 String2",
...            "String1 String3", "String4 String3"]
>>> regex = re.compile(r"^(?=.*String(?:10|3)\b)(?!.*String1\b)")
>>> [string for string in strings if regex.search(string)]
['String10 String2', 'String4 String3']

解释:

regex = re.compile(r"""
        ^         # Match the start of the string
       (?=        # Assert that the following can be matched here:
        .*        # Any string, followed by
        String    # the word "String" and
        (?:10|3)  # either the number 10 or 3.
        \b        # Make sure the word ends here (don't match "String100"!)
       )          # End of lookahead. We're still at the start of the string!
       (?!        # Assert that the following can't be matched here
        .*        # Any string, followed by
        String1   # "String1"
        \b        # Make sure the word ends here (don't match "String10"!)
       )          # End of lookahead
       """, re.VERBOSE)      
于 2013-08-30T15:19:38.950 回答
1

无需为此使用正则表达式。在 Python 中,您可以使用如下内容:

>>> string1 = 'bla'
>>> string2 = 'ble'
>>> string3 = 'blue'
>>> 
>>> the_string = 'blabla'
>>> string1 in the_string and string2 not in the_string and string3 not in the_string
True
于 2013-08-30T15:10:51.633 回答