同一个词的多次点击

2024-04-25 00:31:08 发布

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

我有一个字符串,我想从中提取匹配使用正则表达式。 字符串如下所示:

you and he and she and me

我的正则表达式是(到目前为止):

(\w+) and (\w+)

我想要的是它应该给出这样的结果:

(you, he), (he, she), (she, me)

但目前的结果只包括两个匹配,分别是

(you, he), (she, me)

如何做到这一点?你知道吗


Tags: and字符串youmeheshe
3条回答

您可以使用零宽度正向前瞻,如:

(?=(?:^|\s)(\w+)\s+and\s+(\w+))
  • 零宽度先行模式以(?=开始,最后以)结束

  • (?:^|\s)是一个未捕获的组,确保所需的模式位于开头或后跟空格

  • (\w+)\s+and\s+(\w+),获得第一和第二捕获组的所需模式

示例:

In [11]: s = 'you and he and she and me'

In [12]: re.findall(r'(?=(?:^|\s)(\w+)\s+and\s+(\w+))', s)
Out[12]: [('you', 'he'), ('he', 'she'), ('she', 'me')]

你要的是overlapping regexes。你知道吗

你就是这样做的:

import re                                                                       

s = "you and he and she and me"                                                 

print re.findall(r'(?=\b(\w+) and (\w+)\b)', s)

事实上,它在寻找重叠方面做得非常好,您需要我添加的\b来表示您想要匹配单词边界。否则你会得到:

[('you', 'he'), ('ou', 'he'), ('u', 'he'), ('he', 'she'), ('e', 'she'), ('she', 'me'), ('he', 'me'), ('e', 'me')]

正如其他人所指出的,你所寻找的是所谓的重叠匹配。
使用较新的^{} module,您可以坚持最初的方法并应用另一个标志:

import regex as re

string = "you and he and she and me"
rx = r'\b(\w+) and (\w+)\b'

matches = re.findall(rx, string, overlapped=True)
print matches
# [('you', 'he'), ('he', 'she'), ('she', 'me')]

提示:您需要在顶部设置单词边界(\b),否则会得到意外的结果。你知道吗

相关问题 更多 >