【发布时间】:2017-07-26 15:10:26
【问题描述】:
我试图不匹配 XML 标记之后或之前的单词。
import re
strTest = "<random xml>hello this was successful price<random xml>"
for c in re.finditer(r'(?<![<>])(\b\w+\b)(?<!=[<>])(\W+)',strTest):
c1 = c.group(1)
c2 = c.group(2)
if ('<' != c2[0]) and ('<' != c.group(1)[len(c.group(1))-1]):
print c1
结果是:
xml
this
was
successful
xml
想要的结果:
this
was
successful
我一直在尝试否定前瞻和否定后瞻断言。我不确定这是否是正确的方法,我将不胜感激。
【问题讨论】:
-
Don't use Regexp to parse XML。使用 XML 解析器。
-
A trick 可以是:匹配你不想要的,但capture 匹配你需要的。
\w*\s*<[^>]*>\s*\w*|(\w+)