我有以下正则表达式 ^.*?((\yo\b.*?(cut me:)[\s\S]*))$
- 带有 ms
标志
如您所见,它与“yo”的第一个实例匹配。我希望它匹配最接近“cut me”的“yo”的最后一个实例。我需要它是多行点。
如何修改正则表达式以获得以下匹配的输出?
输出(当前以红色匹配):
yo keep this here
Keep this here
yo
blah, blah, blah cut me:
Allof this here should be deleted
预期输出(我想要以红色匹配的内容):
yo
blah, blah, blah cut me:
Allof this here should be deleted
最佳答案
也许不要让第一个 .*?
不情愿。
^.*((\yo\b.*?(cut me:)[\s\S]*))$
似乎可以解决你的问题。
此外,由于您使用的是 dotall
标志,因此可以将 [\s\S]
更改为 。
关于java - 如何匹配模式正则表达式中 token 的最后一个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22509476/