为什么“2010 年"=~/([0-4]*)/导致 $1 中的空字符串? [英] Why does "Year 2010" =~ /([0-4]*)/ results in empty string in $1?

查看:32
本文介绍了为什么“2010 年"=~/([0-4]*)/导致 $1 中的空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我跑

"Year 2010" =~ /([0-4]*)/;
print $1;

我得到空字符串.但是

"Year 2010" =~ /([0-4]+)/;
print $1;

输出2010".为什么?

outputs "2010". Why?

推荐答案

对于第一个表单,您会在字符串Year 2010"的开头得到一个空匹配项,因为 * 将立即匹配 0 位数字.+ 表单必须等到它看到至少一位数字后才能匹配.

You get an empty match right at the start of the string "Year 2010" for the first form because the * will immediately match 0 digits. The + form will have to wait until it sees at least one digit before it matches.

大概如果你能通过第一个形式的所有匹配,你最终会找到 2010 ......但可能只有在它在 'e' 之前找到另一个空匹配之后,然后在之前'a' 等

Presumably if you can go through all the matches of the first form, you'll eventually find 2010... but probably only after it finds another empty match before the 'e', then before the 'a' etc.

这篇关于为什么“2010 年"=~/([0-4]*)/导致 $1 中的空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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