使用函数解析函数参数的正则表达式 [英] Regular expression for parse function arguments with functions
问题描述
我想获取字符串的函数参数.
I want to get the function arguments of string.
sample( 5*5 ) euros
这适用于:
([^\s\)]+)\(([^\)]+)\)
此处演示.
问题是当我在参数中放入另一个函数时:
The problem is when I put another function inside the argument:
sample( decimal( 5*5 ) ) euros
只有一个函数可以使用:
With only a function this works with:
([^\s\)]+)\((.+)\)
此处演示.
但是有两个或更多函数时,我无法获取函数参数:
But with two functions or more I can't get the function arguments:
sample( decimal( 5*5 ) ) toString(euros)
如何使用正则表达式获取函数参数?.
How can I get the function arguments with a regular expression?.
推荐答案
如果你正在编写一个解析器,你可以不用正则表达式.从教育的角度来看,在 PHP PCRE 正则表达式中,您可以使用递归和子例程调用.
If you are writing a parser you can do without a regex. From the educational point of view, in PHP PCRE regex, you can use recursion and subroutine calls.
看看
(?<name>[^\s()]+)(\((?<body>(?>[^()]++|(?2))*)\))
查看正则表达式演示
name"组将包含函数名称,body"组将包含匹配括号内的内容.
Group "name" will contain the function name and "body" group will hold what is inside the matching parentheses.
请注意,您需要将 (
和 )
添加到 否定字符类 (?<funcion>[^\s()]+)
因为如果你有 sample(decimal(3*3))
这个组会抓取子串直到 )
(sample(decimal
).因此,您需要排除 (
和 )
.
Note you need to add both (
and )
to the negated character class (?<funcion>[^\s()]+)
because in case you have sample(decimal(3*3))
this group will grab the substring up to the )
(sample(decimal
). Thus, you need to exclude both (
and )
.
(\((?(?>[^()]++|(?2))*)\))
部分是一个捕获组(带有 ID=2) 可以通过子例程调用 (?2)
进行递归(即重复"、扩展"多次).
The (\((?<body>(?>[^()]++|(?2))*)\))
part is a capture group (with ID=2) that can be recursed (i.e. "repeated", "expanded" many times) with a subroutine call (?2)
.
匹配
\(
- 一个开放的圆括号(?<body>(?>[^()]++|(?2))*)
- 匹配零个或多个序列的body"组:[^()]++
- 除了(
和)
或 之外的 1+ 个字符(?2)
- 整个\((?(?>[^()]++|(?2))*)\)
子模式
\(
- an open round bracket(?<body>(?>[^()]++|(?2))*)
- Group "body" that matches zero or more sequences of:[^()]++
- 1+ characters other than(
and)
or(?2)
- the whole\((?<body>(?>[^()]++|(?2))*)\)
subpattern
(?2)
子程序调用的必要性(与递归和(?R)
相比)取决于我们需要重复/递归部分模式的事实.The
(?2)
subroutine call necessity (as compared to recursion with(?R)
) is dictated by the fact that we need to repeat/recurse a part of the pattern.由于第 2 组是一个技术性"捕获组,因此对于我们想要真正使用的那些部分使用命名捕获组可能是个好主意.
Since Group 2 is a "technical" capture group, it might be a good idea to use named capture groups for those parts we want to really use.
这篇关于使用函数解析函数参数的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!