如何不贪心多个回溯匹配 [英] How to non-greedy multiple lookbehind matches

查看:57
本文介绍了如何不贪心多个回溯匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Source:    <prefix><content1><suffix1><prefix><content2><suffix2>
Engine:    PCRE

RegEx1:    (?<=<prefix>)(.*)(?=<suffix1>)
RegEx2:    (?<=<prefix>)(.*)(?=<suffix2>)

Result1:   <content1>
Result2:   <content1><suffix1><prefix><content2>

RegEx2所需的结果只是< content2>但这显然是贪婪的. 如何使RegEx2为非贪婪的,并仅使用最后一个匹配的Lookbehind?

The desired result for RegEx2 is just <content2> but it is obviously greedy. How do I make RegEx2 non-greedy and use only the last matching lookbehind?

[我希望我已经从NoteTab语法正确翻译了此内容.我不做 正则表达式编码. < prefix>,< content> & <后缀>术语仅表示任意字符串.只有<" ?< =" lookbehind命令中的有效.]

[I hope I have translated this correctly from the NoteTab syntax. I don't do much RegEx coding. The <prefix>, <content> & <suffix> terms are just meant to represent arbitrary strings. Only the "<" in the "?<=" lookbehind command is significant.]

我怀疑这很简单,但是经过太多小时的搜索, 放弃自己解决问题.

I suspect it is something simple but after too many hours of searching I'm giving up on solving it myself.

感谢您的帮助

艺术

推荐答案

我只是遇到了同样的问题.但就我而言,是

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

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

那是我想要的.

此表达式将匹配<prefix><suffix>之间的字符串联的任何内容,并且不包含子字符串<prefix>. (我想是的.我不太擅长regexp.)

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.)

这篇关于如何不贪心多个回溯匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆