RegEx BackReference to Match Different Values

前端 未结 2 1844
栀梦
栀梦 2020-11-28 16:31

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

This regex looks like :

(\\(\\s*([a-zA-Z]+[0-9]*|[0-9]+|\         


        
相关标签:
2条回答
  • 2020-11-28 17:31

    What language/application are you using this regular expression in? If you have the option you can specify the different parts as named variables and then build the final regular expression by combining them.

    val = "([a-zA-Z]+[0-9]*|[0-9]+|\'.*\'|\[.*\])"
    op = "(ni|in|\*|\/|\+|\-|==|!=|>|>=|<|<=)"
    exp = "(\(" .. val .. "\s*" .. op .. "\s*" .. val .. "\))"
    
    0 讨论(0)
  • 2020-11-28 17:32

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

    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*\))
    

    See the regex demo

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

    0 讨论(0)
提交回复
热议问题