正则表达式先行n次python

发布于 2025-01-09 03:26:51 字数 290 浏览 3 评论 0原文

我以下面的句子为例

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心碎的声音 2025-01-16 03:26:51

您可以简化正则表达式 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

淑女气质 2025-01-16 03:26:51

您可以使用

rgx = r'\bisaac\b|\bmorka\b(?!.*\bmorka\b)'
str = 'isaac morka morka morka'
re.findall(rgx, str)
  #=> ["isaac", "morka"]

Python 演示<-\(ツ)/-> ;正则表达式演示

让我们分解一下正则表达式。

\bisaac\b   # match 'isaac' with word boundaries fore and aft
|           # or
\bmorka\b   # match 'morca'  with word boundaries fore and aft
(?!         # begin negative lookahead
  .*        # match zero or more characters
  \bmorka\b # match 'morca' with word boundaries fore and aft
)           # end negative lookahead


如果要返回唯一单词的列表,使得列表中的每个单词在字符串中至少出现一次,则可以编写以下内容。

str = "isaac morka louie morka isaac morka"

rgx = r'\b(\w+)\b(?!.*\b\1\b)'

re.findall(rgx, str)
  #=> ['louie', 'isaac', 'morka']

<一href="https://tio.run/##K6gsycjPM/7/PzO3IL@oRKEolYuruKRIwVZBKbM4MTFZITe/KDtRISe/NDMVyk YSV@LiKkqvACouUo9J0ogp19YEUvaKeloxSTGGMUma6lxcBUWZeSUaRal6aZl5KYk5ORpADToKQCs0Nbn@/wcA" rel="nofollow noreferrer">演示

\b(\w+)\b   # match one or more word characters with word boundaries
            # fore and aft and save to capture group 1
(?!         # begin negative lookahead
  .*        # match zero or more characters
  \b\1\b    # match the content of capture group 1 with word boundaries
            # fore and aft
)           # end negative lookahead

You can use

rgx = r'\bisaac\b|\bmorka\b(?!.*\bmorka\b)'
str = 'isaac morka morka morka'
re.findall(rgx, str)
  #=> ["isaac", "morka"]

Python demo<-\(ツ)/->Regex demo

Let's break down the regular expression.

\bisaac\b   # match 'isaac' with word boundaries fore and aft
|           # or
\bmorka\b   # match 'morca'  with word boundaries fore and aft
(?!         # begin negative lookahead
  .*        # match zero or more characters
  \bmorka\b # match 'morca' with word boundaries fore and aft
)           # end negative lookahead


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.

str = "isaac morka louie morka isaac morka"

rgx = r'\b(\w+)\b(?!.*\b\1\b)'

re.findall(rgx, str)
  #=> ['louie', 'isaac', 'morka']

Demo

\b(\w+)\b   # match one or more word characters with word boundaries
            # fore and aft and save to capture group 1
(?!         # begin negative lookahead
  .*        # match zero or more characters
  \b\1\b    # match the content of capture group 1 with word boundaries
            # fore and aft
)           # end negative lookahead
七禾 2025-01-16 03:26:51

正则表达式与 @anotherGatsby 使用的相同。下面的代码片段给出了您所需的结果。

x=['艾萨克莫卡莫卡']

str = str(x)

rex =re.compile('isaac[\s\S]+?morka')

打印(re.findall(rex,str))

Regex is same As what @anotherGatsby used. Below code snippet gives your required result.

x=['isaac morka morka']

str = str(x)

rex =re.compile('isaac[\s\S]+?morka')

print(re.findall(rex,str))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文