【问题标题】:How to match a string that a specific word doesn't comes before it?如何匹配特定单词之前没有出现的字符串?
【发布时间】:2021-09-05 16:40:40
【问题描述】:

我尝试匹配字符串 ARIBABA,前提是 .get 没有出现在它之前。

例子:

ARIBABA = config.get('SOMETHING', 'ARIBABA').lower()

我试过这个(在下面),但它什么都不匹配。

^(.get)\bARIBABA\b

【问题讨论】:

  • 正好在它之前?还是字符串中的某个位置?
  • .get 不能出现在 ARIBABA 之前
  • 当有人回答我的问题时我该怎么办? stackoverflow.com/help/someone-answers。确定答案是否有帮助,然后... - 投票 - 接受它

标签: python regex python-re


【解决方案1】:

你可以试试这个:

if 0 =< s.find('.get') < s.find('ARIBABA'):

s.find(substring) 返回以子字符串开头的 s 的最低索引

完整示例:

s = "config.get('SOMETHING', 'ARIBABA').lower()"
if 0 =< s.find('.get') < s.find('ARIBABA'):
    print('.get comes before ARIBABA')

输出:

.get comes before ARIBABA

编辑: 如果 s 中不存在子字符串,find 将返回 -1,这就是我添加0=&lt; 条件的原因

【讨论】:

  • 真的很聪明!!!但是它不适用于这种情况:config.getNOISE('SOMETHING', 'ARIBABA').lower() 但我认为这不是问题。 +1
  • 感谢您的注意,我编辑了我的答案,现在适用于所有情况。
【解决方案2】:

你必须在这里使用 PyPi 正则表达式库:

import regex
s = "ARIBABA = config.get('SOMETHING', 'ARIBABA').lower()"
p = r"(?<!\.get\b.*)\bARIBABA\b"
print(regex.findall(p, s))

Python proof

解释

--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    get                      'get'
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  ARIBABA                  'ARIBABA'
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

【讨论】:

    【解决方案3】:

    这是纯正则表达式解决方案。

    pattern = "^(?!.*\.get).*(ARIBABA)"
    aribaba = "config.get('SOMETHING', 'ARIBABA').lower()"
    try:
        re.search(pattern, aribaba).group(1)
    except:
        print("No ARIBABA or get comes before")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-14
      • 2020-06-26
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      相关资源
      最近更新 更多