Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式rpg函数_Java_Regex - Fatal编程技术网

Java 正则表达式rpg函数

Java 正则表达式rpg函数,java,regex,Java,Regex,(希望写一个好问题) 我必须使用正则表达式匹配Java中的RPG函数。 我的功能有以下几种: nameFunction() nameFunction(params) nameFunction('otherFunction(' + variable + ')') nameFunction('otherFunction.get('''+trim(string_variable)+''')') fun1() + fun2() 其中,'用于打开/关闭字符串,'还用作'本身的转义字符。 我已经尝试使用这

(希望写一个好问题)

我必须使用正则表达式匹配Java中的RPG函数。 我的功能有以下几种:

nameFunction()
nameFunction(params)
nameFunction('otherFunction(' + variable + ')')
nameFunction('otherFunction.get('''+trim(string_variable)+''')')
fun1() + fun2()
其中,'用于打开/关闭字符串,'还用作'本身的转义字符。 我已经尝试使用这个正则表达式:

 \w+\([^\)]*\)
其中与\w+匹配的是名称,然后是(,所有字符,但不包括右括号,最后是右括号。 前两个函数被正确识别,但第三个和第四个函数没有Regex匹配

nameFunction('otherFunction(' + variable + ')
nameFunction('otherFunction.get('''+trim(string_variable)
在第三个中跳过最后一个闭括号,在第四个中跳过字符串连接(第五个示例是为了说明我可以有多个函数,我想分别识别它们)。
有什么建议吗?提前谢谢。

如果字符串没有转义符,您可以使用:

\w+\((?:[^'()]|'[^']*')*\)
如果要处理反斜杠转义,可以使用以下方法:

\w+\((?:[^'()]|'(?:[^'\\]|\\.)*')*\)

您的问题是不允许在函数params中使用右括号

\w+\(.*\)

这就可以了。

如果您只想匹配第一个
和最外层括号之间的所有文本,那么这个基于负前瞻的正则表达式将适用于您:

Pattern p = Pattern.compile("(\\w+)\\s*\\((.*?)\\)(?![^)]*\\))");
与您的全部4个示例一起使用


在线演示:
*
是贪婪的,所以这不起作用,因为如果存在多个头,它不仅匹配头…@Michael Krejci您的正则表达式不能正确匹配多个函数,如
fun1()+fun2()
感谢您的回答。字符串的转义是“char本身,因此我可以使用类似于nameFunction('otherFunction.get(''+string_variable++''))的东西,它实际上与您的正则表达式不匹配。它只匹配get(''+%TRIM(VAR2)+''))@farerobe这个答案适用于你的例子。如果这不是你想要的,请按照它们的样子调整你的例子。不要使用我们必须猜测其预期含义的额外符号。@farerobe,第一个正则表达式匹配
nameFunction('otherFunction.get('+string\u variable+''))
很好..我不知道您所说的“rpg函数”是什么意思,但rpg标记是针对编程语言的,在这里不合适。