正则表达式BackReference以匹配不同的值 [英] RegEx BackReference to Match Different Values

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

问题描述

我有一个正则表达式,用于匹配(val1 operator val2)

I have a regex that I use to match Expression of the form (val1 operator val2)

此正则表达式如下:

(\(\s*([a-zA-Z]+[0-9]*|[0-9]+|\'.*\'|\[.*\])\s*(ni|in|\*|\/|\+|\-|==|!=|>|>=|<|<=)\s*([a-zA-Z]+[0-9]*|[0-9]+|\'.*\'|\[.*\])\s*\))

实际上哪个很好,并且符合我的要求,正如您在本演示中看到的此处

Which is actually good and matches what I want as you can see here in this demo

但是:D(黄油来了)

我想通过使其更易读和紧凑"来优化正则表达式本身.我搜索了该方法,然后找到了一种称为反向引用的名称,您可以在其中命名捕获组,然后像这样引用它们:

I want to optimise the regex itself by making it more readable and "Compact". I searched on how to do that and I found something called back-reference, in which you can name your capturing groups and then reference them later as such:

(\(\s*(?P<Val>[a-zA-Z]+[0-9]*|[0-9]+|\'.*\'|\[.*\])\s*(ni|in|\*|\/|\+|\-|==|!=|>|>=|<|<=)\s*(\g{Val})\s*\))

我在其中命名了捕获表达式Val左侧的组,后来我将其引用为(\g{Val}),现在问题是该表达式,如您所见此处仅在表达式的左侧与右侧完全相同的情况下!例如(a==a)(1==1)且与(a==b)

where I named the group that captures the left side of the expression Val and later I referenced it as (\g{Val}), now the problem is that this expression as you can see here only case where left side of the expression is exactly the same as right side! e.g. (a==a) or (1==1) and does not match expressions such as (a==b)!

现在的问题是:有没有办法引用模式而不是匹配的值?!

Now the question is: is there a way to reference the pattern instead of the matched value?!

推荐答案

请注意,\g{N}等同于\1,即与相同 value ,而不是对应的捕获组匹配的模式.不过,此语法稍微灵活一些,因为您可以通过在数字前使用-来定义相对于当前组相对的捕获组(即\g{-2}(\p{L})(\d)\g{-2}将匹配a1a).

Note that \g{N} is equivalent to \1, that is, a backreference that matches the same value, not the pattern, that the corresponding capturing group matched. This syntax is a bit more flexible though, since you can define the capture groups that are relative to the current group by using - before the number (i.e. \g{-2}, (\p{L})(\d)\g{-2} will match a1a).

PCRE引擎允许 子例程调用 递归子模式.要重复组1的模式,请使用(?1)(?&Val)递归命名组Val的模式.

The PCRE engine allows subroutine calls to recurse subpatterns. To repeat the pattern of Group 1, use (?1), and (?&Val) to recurse the pattern of the named group Val.

此外,您可以使用字符类来匹配单个字符,并考虑使用?量词使正则表达式的某些部分可选:

Also, you may use character classes to match single characters, and consider using ? quantifier to make parts of the regex optional:

(\(\s*(?P<Val>[a-zA-Z]+[0-9]*|[0-9]+|\'.*\'|\[.*\])\s*(ni|in|[*\/+-]|[=!><]=|[><])\s*((?&Val))\s*\))

请参见 regex演示

请注意,\'.*\'\[.*\]可能匹配太多,请考虑用\'[^\']*\'\[[^][]*\]替换.

Note that \'.*\' and \[.*\] can match too much, consider replacing with \'[^\']*\' and \[[^][]*\].

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

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