正则表达式 - 匹配所有出现? [英] Regex - Match all occurrences?

查看:66
本文介绍了正则表达式 - 匹配所有出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

my @matches = ($result =~ m/INFO\n(.*?)\n/);

所以在 Perl 中,我想存储与该正则表达式的所有匹配项.我希望在每次发生时将值存储在 INFO\n 和 \n 之间.

So in Perl I want to store all matches to that regular expression. I'm looking to store the value between INFO\n and \n each time it occurs.

但我只存储了最后一次出现的情况.我的正则表达式错了吗?

But I'm only getting the last occurrence stored. Is my regex wrong?

推荐答案

使用 /g 修饰符进行全局匹配.

Use the /g modifier for global matching.

my @matches = ($result =~ m/INFO\n(.*?)\n/g);

在这种情况下不需要延迟量化,因为 . 不匹配换行符.以下将提供更好的性能:

Lazy quantification is unnecessary in this case as . doesn't match newlines. The following would give better performance:

my @matches = ($result =~ m/INFO\n(.*)\n/g);

/s 如果您确实希望句点与换行符匹配,则可以使用它.有关这些修饰符的更多信息,请参阅 perlre.

/s can be used if you do want periods to match newlines. For more info about these modifiers, see perlre.

这篇关于正则表达式 - 匹配所有出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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