正则表达式先行n次python
我以下面的句子为例
isaac morka morka morka
我试图得到以下结果:
isaac morka
我尝试了以下代码:
re.findall(r'isaac[\s\w]+(?=morka)', 'isaac morka morka morka')
但获得的结果不正确
['isaac morka morka']
I have the following sentence as an example
isaac morka morka morka
I am trying to get the following result:
isaac morka
i tried the following code:
re.findall(r'isaac[\s\w]+(?=morka)', 'isaac morka morka morka')
but the result obtained is not correct
['isaac morka morka']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以简化正则表达式
isaac[\s\S]+?morka
而不使用环视。在此处测试正则表达式:https://regex101.com/r/bmp5pH/1
You can simplify your regex
isaac[\s\S]+?morka
without using lookaround.Test the regex here: https://regex101.com/r/bmp5pH/1
您可以使用
Python 演示<-\(ツ)/-> ;正则表达式演示
让我们分解一下正则表达式。
如果要返回唯一单词的列表,使得列表中的每个单词在字符串中至少出现一次,则可以编写以下内容。
<一href="https://tio.run/##K6gsycjPM/7/PzO3IL@oRKEolYuruKRIwVZBKbM4MTFZITe/KDtRISe/NDMVyk YSV@LiKkqvACouUo9J0ogp19YEUvaKeloxSTGGMUma6lxcBUWZeSUaRal6aZl5KYk5ORpADToKQCs0Nbn@/wcA" rel="nofollow noreferrer">演示
You can use
Python demo<-\(ツ)/->Regex demo
Let's break down the regular expression.
If a list of unique words is to be returned such that each word in the list appears at least once in the string one could write the following.
Demo
正则表达式与 @anotherGatsby 使用的相同。下面的代码片段给出了您所需的结果。
Regex is same As what @anotherGatsby used. Below code snippet gives your required result.