我如何才能捕获存储从一个Perl的正前pression成独立的变量? [英] How can I store captures from a Perl regular expression into separate variables?

查看:138
本文介绍了我如何才能捕获存储从一个Perl的正前pression成独立的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式:

  / ABC(DEF)GHI(JKL)MNO(PQR)/ IGS

我将如何捕捉每一个括号的结果分为3个不同的变量,每一个括号?我用一个阵列来捕捉所有的结果,现在,他们出来的顺序,但后来我不得不解析它们,这个名单可以是巨大的。

  @Results =($字符串=〜/ ABC(DEF)GHI(JKL)MNO(PQR)/ IGS);


解决方案

您的问题是一个有点暧昧给我,但我觉得你想要做这样的事情:

 我的(@First,@second,@third);
而(我的(第一个$,$第二个,第三个$)= $字符串=〜/ ABC(DEF)GHI(JKL)MNO(PQR)/ IGS){
    推@First,$​​第一;
    推@second,$第二;
    推@third,$第三;
}

I have a regex:

/abc(def)ghi(jkl)mno(pqr)/igs

How would I capture the results of each parentheses into 3 different variables, one for each parentheses? Right now I using one array to capture all the results, they come out sequential but then I have to parse them and the list could be huge.

@results = ($string =~ /abc(def)ghi(jkl)mno(pqr)/igs);

解决方案

Your question is a bit ambiguous to me, but I think you want to do something like this:

my (@first, @second, @third);
while( my ($first, $second, $third) = $string =~ /abc(def)ghi(jkl)mno(pqr)/igs) {
    push @first, $first;
    push @second, $second;
    push @third, $third;
}

这篇关于我如何才能捕获存储从一个Perl的正前pression成独立的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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