如何匹配前面没有特定单词的字符串?
How to match a string that a specific word doesn't comes before it?
我试图匹配字符串 ARIBABA 仅当 .get 不在它之前时。
示例:
ARIBABA = config.get('SOMETHING', 'ARIBABA').lower()
我试过这个(在下面),但它与任何东西都不匹配。
^(.get)\bARIBABA\b
你可以简单地试试这个:
if 0 =< s.find('.get') < s.find('ARIBABA'):
s.find(substring)
returns 以子字符串
开头的 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 将 return -1,这就是我添加 0=<
条件
的原因
这是纯正则表达式的解决方案。
pattern = "^(?!.*\.get).*(ARIBABA)"
aribaba = "config.get('SOMETHING', 'ARIBABA').lower()"
try:
re.search(pattern, aribaba).group(1)
except:
print("No ARIBABA or get comes before")
您必须在此处使用 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
我试图匹配字符串 ARIBABA 仅当 .get 不在它之前时。
示例:
ARIBABA = config.get('SOMETHING', 'ARIBABA').lower()
我试过这个(在下面),但它与任何东西都不匹配。
^(.get)\bARIBABA\b
你可以简单地试试这个:
if 0 =< s.find('.get') < s.find('ARIBABA'):
s.find(substring)
returns 以子字符串
完整示例:
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 将 return -1,这就是我添加 0=<
条件
这是纯正则表达式的解决方案。
pattern = "^(?!.*\.get).*(ARIBABA)"
aribaba = "config.get('SOMETHING', 'ARIBABA').lower()"
try:
re.search(pattern, aribaba).group(1)
except:
print("No ARIBABA or get comes before")
您必须在此处使用 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