正则表达式超前 [英] Regex lookahead

查看:79
本文介绍了正则表达式超前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用正则表达式查找:

I am using a regex to find:

test:?

后跟任何字符直到都会打到下一个字符:

Followed by any character until it hits the next:

test:?

现在,当我运行此正则表达式时,我做了:

Now when I run this regex I made:

((?:test:\?)(.*)(?!test:\?))

在此文本上:

test:?foo2=bar2&baz2=foo2test:?foo=bar&baz=footest:?foo2=bar2&baz2=foo2

我希望得到:

test:?foo2=bar2&baz2=foo2

test:?foo=bar&baz=foo

test:?foo2=bar2&baz2=foo2

但它匹配所有内容.有更多具有正则表达式经验的人知道我哪里出错了吗?我以前使用过正则表达式进行模式匹配,但这是我第一次环视/超前体验.

But instead it matches everything. Does anyone with more regex experience know where I have gone wrong? I've used regexes for pattern matching before but this is my first experience of lookarounds/aheads.

在此先感谢您的帮助/提示/指针:-)

Thanks in advance for any help/tips/pointers :-)

推荐答案

我想您可以探索一个贪婪的版本.
(扩展)

I guess you could explore a greedy version.
(expanded)

(test:\? (?: (?!test:\?)[\s\S])* )

这篇关于正则表达式超前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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