[英]PERL How to match last n digits of a string with n consecutive digits or more?
我使用以下內容:
if ($content =~ /([0-9]{11})/) {
my $digits = $1;
}
從字符串中提取 11 個連續數字。 但是,它會抓取前 11 個連續數字。 如何讓它提取最后 11 個連續數字,以便從帶有 hdjf95724555199361 的字符串中得到 24555199361?
/([0-9]{11})/
方法
/^.*?([0-9]{11})/s # Minimal lead that allows a match.
通過使.*
變得貪婪,你得到了你想要的。
/^.*([0-9]{11})/s # Maximal lead that allows a match.
每當您想匹配字符串末尾的某些內容時,請使用行尾錨$
。
$content =~ m/(\d{11})$/;
如果該模式不是最后一次,但您想匹配該模式的“最后一次”出現,則首先將“整個字符串”與/.*/
匹配,然后回溯到該模式的最后一次出現。 /s
標志允許.
匹配換行符的元字符。
$content =~ m/.*(\d{11})/s;
有關詳細信息,請參閱Perl 正則表達式教程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.