gpt4 book ai didi

java - 正则表达式提取最后一次出现后的行

转载 作者:行者123 更新时间:2023-12-02 06:48:43 24 4
gpt4 key购买 nike

嗨,我有一个这样的段落:

            output 123

Deepak everywhere
Deepak where are

output 123

Ankur Everywhere
Deepak where are

last

Deepak everywhere
Deepak where are

我想在最后一次出现“output 123”之后提取到“last”。这就是我所期望的:

            Ankur Everywhere
Deepak where are

last

我使用这个正则表达式模式 - (?<=(output))([^\\n]*)last 。但使用这个,我得到的是:

            output 123

Deepak everywhere
Deepak where are

output 123

Ankur Everywhere
Deepak where are

last

有人可以帮忙吗?我使用这个工具 - http://regexr.com?360ek

最佳答案

您可以使用此模式并提取第一个捕获组:

output\\b[^\\n]*\\s*((?>[^o\\s]++|\\s++(?!last\\b)|o(?!utput\\b))++)(?=\\s+last\b)

详情:

output\\b[^\\n]*\\s* # the begining (exclude from the final result
# but used as an anchor)
( # open the capturing group
(?> # open an atomic group (all the possible content)
[^o\\s]++ # all that is not a "o" or a white character
| # OR
\\s++(?!last\\b) # white characters but not followed by "last"
# (here the possessive quantifier is needed to forbid
# backtracks)
| # OR
o(?!utput\\b) # "o" not followed by "utput\b"
)++ # repeat the atomic group one or more times
) # close the capturing group
(?=\\s+last\b) # followed by white characters and "last"

您可以通过以下方式找到捕获组的内容:m.group(1)

关于java - 正则表达式提取最后一次出现后的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18301823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com