如何将 Perl 正则表达式中的捕获存储到单独的变量中? [英] How can I store captures from a Perl regular expression into separate variables?

查看:42
本文介绍了如何将 Perl 正则表达式中的捕获存储到单独的变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式:

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

我如何将每个括号的结果捕获到 3 个不同的变量中,每个括号一个?现在我使用一个数组来捕获所有结果,它们按顺序出现,但随后我必须解析它们并且列表可能很大.

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 正则表达式中的捕获存储到单独的变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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