How to non-greedy multiple lookbehind matches

孤人 提交于 2019-11-29 05:22:00
5gon12eder

I just had the same problem. But in my case it was

(?<=<prefix>)(?:.(?!<prefix>))*(?=<suffix>)

That did what I wanted.

This expression will match anything that is a concatenation of characters between <prefix> and <suffix> and doesn't contain the substring <prefix>. (I think so. I'm not very good at regexp.)

I suggest you use:

(?<=<prefix>)(((?!<prefix>).)*)(?=<suffix2>)

This makes sure that there can be no <prefix> inside the match. The complete match result will be <content2>

Put something greedy in front of it?

(?:.*)(?<=<prefix>)(.*)(?=<suffix2>)

Since the greedy (?:.*) will gobble as much as it can, only the minimum will be matched by the rest of the pattern - effectively making the rest non-greedy.

The non-greedy .*? might also work:

(?<=<prefix>)(.*?)(?=<suffix2>)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!