Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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
Regex Perl正则表达式匹配_Regex_Perl_Braces - Fatal编程技术网

Regex Perl正则表达式匹配

Regex Perl正则表达式匹配,regex,perl,braces,Regex,Perl,Braces,以下字符串-匹配: "MNO(A=(B=C) D=(E=F)) PQR(X=(G=H) I=(J=(K=L)))" - "MNO" "MNO(A=(B=C) D=(E=F))" - "MNO" "MNO" - "MNO" "RAX.MNO(A=(B=C) D=(E=F)) PQR(X=(G=H) I=(J=(K=L)))" - "RAX.MNO" "RAX.MNO(A=(B=C) D=(E=F))" - "RAX.MNO" "RAX.MNO" - "RAX.MNO" 在每个大括号内可以有无限组

以下字符串-匹配:

"MNO(A=(B=C) D=(E=F)) PQR(X=(G=H) I=(J=(K=L)))" - "MNO"
"MNO(A=(B=C) D=(E=F))" - "MNO"
"MNO" - "MNO"
"RAX.MNO(A=(B=C) D=(E=F)) PQR(X=(G=H) I=(J=(K=L)))" - "RAX.MNO"
"RAX.MNO(A=(B=C) D=(E=F))" - "RAX.MNO"
"RAX.MNO" - "RAX.MNO"
在每个大括号内可以有无限组大括号,但它们必须正确闭合

有什么想法吗?不知道如何正确测试亲密度


例如,在Perl或PHP中必须使用Perl正则表达式

/\((?:[^()]++|(?R))*\)/
匹配平衡圆括号及其内容

在电视上看

要在Perl中删除字符串
$subject
中的所有匹配项,可以使用

$subject =~ s/\((?:[^()]++|(?R))*\)//g;
说明:


你确定要用正则表达式执行此操作吗?这将是递归正则表达式,因此,它不是解决问题的最佳方法。通过堆栈使用经典解决方案要容易得多。这在某些正则表达式风格中是可能的。你用的是哪一个?@我可能要用Regex@RaphaelH祝你好运,但这不是最好的做法,正如之前的评论所说,这取决于味道。Net可以使用平衡组来完成它,请参阅以获得非常好的概述。@RaphaelH:这个正则表达式在Perl或PCRE(Perl兼容的正则表达式)中工作。+1很好。这一点也在本文中提到。如果能有更详细的解释就好了。
\(       # Match a (
(?:      # Start of non-capturing group:
 [^()]++ # Either match one or more characters except (), don't backtrack 
|        # or
 (?R)    # Match the entire regex again, recursively
)*       # Any number of times
\)       # Match a )