使用OR禁止可变长度lookbehind的正则表达式技术 [英] Regex technique to disallow variable length lookbehind using OR

查看:98
本文介绍了使用OR禁止可变长度lookbehind的正则表达式技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解正则表达式引擎如何为下面描述的技术禁止变量长度后视。这通常用于创建具有特定单词但不得包含前一个单词的模式。

I would like to learn how the regex engine works to disallow a variable lenght lookbehind for the below described technique. This is commonly used to create patterns that has a specific word but mustn't have a previous one.

例如,在这篇文章中:与不同于另一个词的单词匹配的ReExEx 想法是匹配单词cube,只有当前20个字符中没有单词时才会出现。

For example, in this post: RegEx that matches a word that NOT succeeds another one the idea is to match the word cube, only if the word small is not present in the previous 20 character.

所以, anubhava 为此问题提供的答案是:

So, the answer provided by anubhava to this question was:

.*?small.{0,20}cube|(.*?cube)

他的评论是:


实际上这是绕过正则表达式引擎的
功能来禁止变量的简单技巧长度看后面。在这个正则表达式中,我们
匹配任何我们不需要使用管道(OR)构造在左侧
侧,最后使用
捕获组在管道中保留最右边的匹配。

Actually this is simple technique to circumvent the regex engine's capabilities to disallow variable length lookbehind. In this regex we match whatever we don't need using pipe (OR) construct on left hand side and finally leave the right most match in the pipe using a captured group.

我认为这种技术非常有用,但不知道如何使用它。我想了解正则表达式引擎如何创建这种正则表达式。任何人都可以帮我解释一下吗?

I think this technique is very useful but don't know how to use it. I would like to understand how the regex engine works to create this kind of regex. Can anybody give me a hand on this explaining me that?

顺便说一句,不知道这个技术是否适用于所有的正则表达式引擎,所以我用java标记了这个问题,因为我主要使用它。

BTW, don't know if this techinique works in all regex engines, so I've labeled the question with java since I'll use it mainly on it.

推荐答案

这个想法是优先选择负数与正数的匹配,所以如果负数匹配,那么它确实匹配,并且你没有得到第一组中的正面匹配。

The idea is to prefer a match of the negative over the positive, so if the negative would match, then it does match, and you don’t get the positive match in the first group.

它实际上并没有以相同的方式工作;如果是的话,正则表达式引擎会使用它。

It doesn’t actually work in the same way, though; if it did, regular expression engines would use it.

put the cube on top of the small cube
        ^ should match cube, but doesn’t, since smallcube is preferred

这篇关于使用OR禁止可变长度lookbehind的正则表达式技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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