简体   繁体   English

正则表达式捕获字符串中的嵌套函数

[英]Regex to capture nested functions in a string

I want to capture all the function names present in an expresison, for example:我想捕获表达式中存在的所有函数名称,例如:

DECODE (TRUE, NOT ISNULL (parameter1),VARIABLE1, ISNULL (Parameter2) AND NOT ISNULL (parameter1),Variable3, ISNULL (parameter) AND ISNULL (parameter) AND NOT ISNULL (parameter),VARIABLE2) DECODE (TRUE, NOT ISNULL (parameter1),VARIABLE1, ISNULL (Parameter2) AND NOT ISNULL (parameter1),Variable3, ISNULL (parameter) AND ISNULL (parameter) AND NOT ISNULL (parameter),VARIABLE2)

I had tried我试过了

([a-zA-Z0-9]+\s*\(([\w\s\,]+)?\)\s*)+

The regex is capturing all the function names except DECODE .正则表达式正在捕获除DECODE之外的所有函数名称。 Could anyone tell what mistake I'm doing?谁能告诉我在做什么错误?

Please try this one请试试这个

([a-zA-Z0-9]+\s*)(\(.+?\))

Basically, it capture 2 groups: before the ( and inside (*) . You can choose which group to extract later. You can watch the demo in here: https://regex101.com/r/9ILSYn/1基本上,它捕获 2 个组:在(之前和内部(*) 。您可以选择稍后提取哪个组。您可以在此处观看演示: https : //regex101.com/r/9ILSYn/1

Or you can just put in the ?: in the later group to mark it as a non-capturing group或者您可以将?:放在后面的组中以将其标记为非捕获组

([a-zA-Z0-9]+\s*)(?:\(.+?\))

In your before-edit regex, you didn't split the capturing groups so it mixed the first function with the parameters.在编辑前的正则表达式中,您没有拆分捕获组,因此将第一个函数与参数混合在一起。

In your updated regex, you are capturing the groups with word and white space inside the (*) , which is not including some character like ( , so it didn't capture the DECODE在您更新的正则表达式中,您在(*)中捕获带有单词和空格的组,其中不包括诸如(字符,因此它没有捕获DECODE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM