【发布时间】:2017-11-30 15:44:33
【问题描述】:
我查看了很多类似标题的帖子,但我没有发现任何适用于 python 甚至这个网站的帖子:https://regex101.com
我怎样才能匹配除特定文本之外的所有内容?
我的文字:
1234_This is a text Word AB
Protocol Address ping
Internet 1.1.1.1 -
Internet 1.1.1.2 25
Internet 1.1.1.3 8
Internet 1.1.1.4 -
1234_This is a text Word BCD
Protocol Address ping
Internet 2.2.2.1 10
Internet 2.2.2.2 -
我想匹配Word \w+,然后匹配其余的直到下一个 1234。
所以结果应该是(返回组标记在()):
(1234_This is a text (Word AB))(
Protocol Address ping
Internet 1.1.1.1 -
Internet 1.1.1.2 25
Internet 1.1.1.3 8
Internet 1.1.1.4 -
)(1234_This is a text (Word BCD)(
Protocol Address ping
Internet 2.2.2.1 10
Internet 2.2.2.2 - )
第一部分很简单:matches = re.findall(r'1234_This is a text (Word \w+)', var)
但下一部分我无法实现。
我尝试过消极的前瞻:
^(?!1234) 但随后它不再匹配...
【问题讨论】: