Python中的向前和向后查找

2024-05-21 09:24:14 发布

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

我对Python中的lookaround有一个问题:

>>> spacereplace = re.compile(b'(?<!\band)(?<!\bor)\s(?!or\b)(?!and\b)', re.I)
>>> q = "a b (c or d)"    
>>> q = spacereplace.sub(" and ", q)
>>> q
# What is meant to happen:
'a and b and (c or d)'

# What instead happens
'a and b and (c and or and d)'

正则表达式应该匹配任何不在单词“and”或“or”旁边的空格,但这似乎不起作用。你知道吗

有人能帮我吗?你知道吗

编辑:作为对注释者的回应,我将regex分解为多行。你知道吗

(?<!\band) # Looks behind the \s, matching if there isn't a word break, followed by "and", there.
(?<!\bor)  # Looks behind the \s, matching if there isn't a word break, followed by "or", there.
\s         # Matches a single whitespace character.
(?!or\b)   # Looks after the \s, matching if there isn't the word "or", followed by a word break there.
(?!and\b)  # Looks after the \s, matching if there isn't the word "and", followed by a word break there.

Tags: orandtherebandbyifword
1条回答
网友
1楼 · 发布于 2024-05-21 09:24:14

您可能将原始字符串修饰符rb混淆了。你知道吗

>>> import re
>>> spacereplace = re.compile(r'(?<!\band)(?<!\bor)\s(?!or\b)(?!and\b)', re.I)
>>> q = "a b (c or d)"
>>> spacereplace.sub(" and ", q)
'a and b and (c or d)' 

有时,如果regexp不起作用,那么使用re.DEBUG标志DEBUG可能会有所帮助。在这种情况下,您可能会注意到,没有检测到单词boundary\b,这可能会提示您在何处搜索错误:

>>> spacereplace = re.compile(b'(?<!\band)(?<!\bor)\s(?!or\b)(?!and\b)', re.I | re.DEBUG)
assert_not -1
  literal 8
  literal 97
  literal 110
  literal 100
assert_not -1
  literal 8
  literal 111
  literal 114
in
  category category_space
assert_not 1
  literal 111
  literal 114
  literal 8
assert_not 1
  literal 97
  literal 110
  literal 100
  literal 8


>>> spacereplace = re.compile(r'(?<!\band)(?<!\bor)\s(?!or\b)(?!and\b)', re.I | re.DEBUG)
assert_not -1
  at at_boundary
  literal 97
  literal 110
  literal 100
assert_not -1
  at at_boundary
  literal 111
  literal 114
in
  category category_space
assert_not 1
  literal 111
  literal 114
  at at_boundary
assert_not 1
  literal 97
  literal 110
  literal 100
  at at_boundary

相关问题 更多 >