如何在Perl中匹配的括号之间提取字符串? [英] How can I extract a string between matching braces in Perl?

查看:1057
本文介绍了如何在Perl中匹配的括号之间提取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入文件如下:

HEADER 
{ABC|*|DEF {GHI 0 1 0} {{Points {}}}}

{ABC|*|DEF {GHI 0 2 0} {{Points {}}}}

{ABC|*|XYZ:abc:def {GHI 0 22 0} {{Points {{F1 1.1} {F2 1.2} {F3 1.3} {F4 1.4}}}}}

{ABC|*|XYZ:ghi:jkl {JKL 0 372 0} {{Points {}}}}

{ABC|*|XYZ:mno:pqr {GHI 0 34 0} {{Points {}}}}

{
    ABC|*|XYZ:abc:pqr {GHI 0 68 0}
        {{Points {{F1 11.11} {F2 12.10} {F3 14.11} {F4 16.23}}}}
        }
TRAILER

我想将文件提取到一个数组中,如下所示:

I want to extract the file into an array as below :

$array[0] = "{ABC|*|DEF {GHI 0 1 0} {{Points {}}}}"

$array[1] = "{ABC|*|DEF {GHI 0 2 0} {{Points {}}}}"

$array[2] = "{ABC|*|XYZ:abc:def {GHI 0 22 0} {{Points {{F1 1.1} {F2 1.2} {F3 1.3} {F4 1.4}}}}}"

..
..

$array[5] = "{
    ABC|*|XYZ:abc:pqr {GHI 0 68 0}
        {{Points {{F1 11.11} {F2 12.10} {F3 14.11} {F4 16.23}}}}
        }"

这意味着,我需要将第一个打开的花括号与它的关闭的花括号匹配,并提取两者之间的字符串.

Which means, I need to match the first opening curly brace with its closing curly brace and extract the string in between.

我已经检查了以下链接,但这不适用于我的问题. 获取字符串的正则表达式在花括号之间"{{我想在花括号之间是什么}"

I have checked the below link, but this doesnt apply to my question. Regex to get string between curly braces "{I want what's between the curly braces}"

我正在尝试,但是如果有人可以用他们的专业知识来帮助我,那将真的有帮助...

I am trying but would really help if someone can assist me with their expertise ...

谢谢 斯里...

推荐答案

至少在现代版本的Perl中,可以使用正则表达式当然可以做到这一点:

This can certainly be done with regex at least in modern versions of Perl:

my @array = $str =~ /( \{ (?: [^{}]* | (?0) )* \} )/xg;

print join "\n" => @array;

正则表达式匹配包含大括号字符或包含递归自身的大括号块(匹配嵌套大括号)

The regex matches a curly brace block that contains either non curly brace characters, or a recursion into itself (matches nested braces)

上面的代码在Perl 5.10+中有效,对于较早的版本,递归更加冗长:

the above code works in Perl 5.10+, for earlier versions the recursion is a bit more verbose:

my $re; $re = qr/ \{ (?: [^{}]* | (??{$re}) )* \} /x;

my @array = $str =~ /$re/xg;

这篇关于如何在Perl中匹配的括号之间提取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆