如何匹配 perl 正则表达式中最后一次出现某个字符之后的所有内容? [英] How can I match everything that is after the last occurrence of some char in a perl regular expression?

查看:75
本文介绍了如何匹配 perl 正则表达式中最后一次出现某个字符之后的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如返回axxxghdfx445中最后一个x之后的字符串部分(应返回445).

For example, return the part of the string that is after the last x in axxxghdfx445 (should return 445).

推荐答案

my($substr) = $string =~ /.*x(.*)/;

来自 perldoc perlre:

From perldoc perlre:

默认情况下,一个量化的子模式是贪婪的",即会匹配尽可能多的次数(给定一个特定的起始位置)同时仍然允许模式的其余部分匹配.

By default, a quantified subpattern is "greedy", that is, it will match as many times as possible (given a particular starting location) while still allowing the rest of the pattern to match.

这就是为什么 .*x 将匹配到 x 的最后一次出现.

That's why .*x will match up to the last occurence of x.

这篇关于如何匹配 perl 正则表达式中最后一次出现某个字符之后的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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