gpt4 book ai didi

python - 匹配一个单词但仅当另一个单词没有出现时的正则表达式?

转载 作者:行者123 更新时间:2023-12-04 08:37:14 25 4
gpt4 key购买 nike

我通常对正则表达式非常好,但我在这个问题上苦苦挣扎。我需要一个匹配术语 cbd 的正则表达式但如果这句话 central business district出现在搜索字符串的任何其他位置。或者如果这太难了,至少匹配 cbd如果短语 central business district不会出现在术语 cbd 之前的任何位置.只有 cbd部分应该作为结果返回,所以我正在使用前瞻/后视,但我无法满足要求......
输入示例:
好的Any products containing CBD are to be regulated.坏    Properties located within the Central Business District (CBD) are to be regulated我试过了:

  • (?!central business district)cbd
  • (.*(?!central business district).*)cbd

  • 这是在 Python 3.6+ 中使用 re模块。
    我知道用几行代码很容易完成,但是我们在数据库中有一个正则表达式字符串列表,我们用它来搜索语料库以查找包含数据库中任何一个正则表达式字符串的文档。最好避免将任何关键字硬编码到脚本中,因为这样我们的其他开发人员将不清楚这些匹配来自何处,因为他们无法在数据库中看到它。

    最佳答案

    使用 PyPi 正则表达式

    import regex
    strings = [' I need a regular expression that matches the term cbd but not if the phrase central business district appears anywhere else in the search string.', 'I need cbd here.']
    for s in strings:
    x = regex.search(r'(?<!central business district.*)cbd(?!.*central business district)', s, regex.S)
    if x:
    print(s, x.group(), sep=" => ")
    结果: I need cbd here. => cbd .见 Python code .
    解释
    --------------------------------------------------------------------------------
    (?<! look behind to see if there is not:
    --------------------------------------------------------------------------------
    central business 'central business district'
    district
    --------------------------------------------------------------------------------
    .* any character except \n (0 or more times
    (matching the most amount possible))
    --------------------------------------------------------------------------------
    ) end of look-behind
    --------------------------------------------------------------------------------
    cbd 'cbd'
    --------------------------------------------------------------------------------
    (?! look ahead to see if there is not:
    --------------------------------------------------------------------------------
    .* any character except \n (0 or more times
    (matching the most amount possible))
    --------------------------------------------------------------------------------
    central business 'central business district'
    district
    --------------------------------------------------------------------------------
    ) end of look-ahead

    关于python - 匹配一个单词但仅当另一个单词没有出现时的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64742598/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com