如何匹配模式正则表达式中的最后一个令牌实例?

时间:2014-03-19 14:45:37

标签: java regex pattern-matching

我有以下正则表达式^.*?((\yo\b.*?(cut me:)[\s\S]*))$ - 带有ms标记

regex101 example

正如您所看到的,它匹配" yo"的第一个实例。我希望它匹配" yo"的最后一个实例。最接近"切断我"。我需要这个是multiline-dotall。

如何修改正则表达式以便获得以下匹配的输出?

输出(目前以红色匹配):

yo keep this here

Keep this here


yo 
blah, blah, blah cut me:

Allof this here should be deleted

Exptected Output(我希望以红色匹配):

yo 
blah, blah, blah cut me:

Allof this here should be deleted

1 个答案:

答案 0 :(得分:2)

也许不要先让.*?不情愿。

^.*((\yo\b.*?(cut me:)[\s\S]*))$

似乎解决了你的问题。

此外,由于您使用的是dotall标记,因此可能会将[\s\S]更改为.