如何在Python中包括两次彼此重叠的多字正则匹配
我目前的文本如下:
为了在我们的政治中进行更多的犬儒主义和两极分化,现在没有快速解决这个长期趋势,我同意我们的贸易不仅是公平的,而且不仅是免费的,而且是下一步的经济脱位不会来自海外,这将来自无情的自动化速度,这使很多好的中产阶级工作都过时了,因此我们将不得不建立一个新的社交紧凑型,以确保我们所有的孩子
我想找到匹配的匹配项,在搜索的正则搜索正则
正则搜索的正则是\ b(com(es?| ing)|来)\ b
搜索 我要寻找的比赛将是
经济错位不会来自海外
和 海外它将来自不懈的
,
因此我设计了一个以下等级,其中包括特定规则 \ w+'?\ w*\ s \ s \ w+'?\ w*\ s \ s \ w+'?\ w*\ s \ b(com(es?| in)|来)\ b \ s \ s \ w+' ?\ w*\ s \ w+'?\ w*\ s \ s \ w+'?\ w*
,
但两个结果彼此重叠,最终仅以1重叠,这是第一场比赛。
需要如何更改我的正则表达式以包括重叠结果?
I currently have a text as follows:
for more cynicism and polarization in our politics now there're no quick fixes to this long-term trend i agree our trade should be fair and not just free but the next wave of economic dislocations won't come from overseas it will come from the relentless pace of automation that makes a lot of good middle class jobs obsolete and so we're going to have to forge a new social compact to guarantee all our kids the
so I would like to find matches that include 3 words both in front and behind the searched regex
The regex that I would like to search is \b(com(es?|ing)|came)\b
so the matches that I will be looking for will be
economic dislocations won't come from overseas it
andoverseas it will come from the relentless
so I've devised a regex that includes the specific rules\w+'?\w*\s\w+'?\w*\s\w+'?\w*\s\b(com(es?|ing)|came)\b\s\w+'?\w*\s\w+'?\w*\s\w+'?\w*
but the two results overlap with each other and end up just with 1, which is the first match.
How do need to change my regex to include overlapping results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要确保
\ w
和之前使用单词边界,并且 - 捕获组量化了三次。
请参阅 regex demo :
请参阅 python demo :
输出:
You need to make sure
\w
andBesides, you can shorten the pattern if you use a non-capturing group quantified three times.
See the regex demo:
See the Python demo:
Output: