Perl 遍历每个匹配项 [英] Perl iterate through each match

查看:37
本文介绍了Perl 遍历每个匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在浏览原始 html 页面以查找此正则表达式.(末尾的引号是故意的).

Let's say I'm scanning through a page of raw html looking for this regex. (The quote mark on the end is intentional).

m/(https?:\/\/.*?(?:'|"))/

此模式可能匹配约 100 次.什么是常见的 perl 习惯用法/迭代所有捕获组匹配项列表的快速方法?

This pattern is likely to match ~ 100 times. What is a common perl idiom/a quick way to iterate through a list of all capture group matches?

推荐答案

来自 perlretut(非常好的教程)

From the perlretut (a very fine tutorial)

while ($x =~ /(\w+)/g) {
        print "Word is $1, ends at position ", pos $x, "\n";
    }

您可以使用 whileg 修饰符来遍历所有匹配项,使用 $1 您可以获得捕获组 1 的内容,在本教程的示例中,您还可以通过 pos 获得位置.

You can use while together with the g modifier to iterate over all matches, with $1 you get the content of your capturing group 1, and in this example from the tutorial you get also the position with pos.

这篇关于Perl 遍历每个匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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