可变长度后向 [英] Variable length look-behind

查看:108
本文介绍了可变长度后向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何优雅的解决方案来构建这种长度可变的后向正则表达式?

Is there any elegant solution to build a variable length look-behind regex such as this one ?

/(?<=eat_(apple|pear|orange)_)today|yesterday/g;

似乎Perl具有非常令人印象深刻的正则表达式引擎,而后面的可变长度外观将非常有趣.有什么方法可以使它起作用,还是我应该忘记这个坏主意?

It seems Perl has a very impressive regex engine and variable length lookbehind would be very interesting. Is there a way to make it work or should I forget this bad idea ?

推荐答案

使用\K作为特例.

这是一个可变长度的正向断言:

It's a variable length positive lookbehind assertion:

/eat_(?:apple|pear|orange)_\Ktoday|yesterday/g

或者,您可以单独列出后置断言:

Alternatively, you can list out your lookbehind assertions separately:

/(?:(?<=eat_apple_)|(?<=eat_pear_)|(?<=eat_orange_))today|yesterday/g

但是,我建议这将是一个罕见的问题,可以使用该功能,但不能重新考虑使用其他更常见的正则表达式功能的组合.

However, I would propose that it's going to be a rare problem that could potentially use that feature, but couldn't be rethought to use a combination of other more common regex features.

换句话说,如果您遇到了特定问题,请随时在此处分享,我相信有人可以提出不同的(也许更好)的方法.

In other words, if you get stuck on a specific problem, feel free to share it here, and I'm sure someone can come up with a different (perhaps better) approach.

这篇关于可变长度后向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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