首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

perl中的$_$1,$2是如何定义的

2012-02-07 
perl中的$_,$1,$2是怎么定义的$_“He’s out bowling with Barney tonight.”s/Barney/Fred/#Barney被Fred

perl中的$_,$1,$2是怎么定义的
$_=“He’s out bowling with Barney tonight.”;
s/Barney/Fred/;#Barney被Fred替换掉
print“$_\n”;


s/with(\w+)/agaist$1’s team/;
print“$_\n”; #为“He’s out bowling against Fred’s team tonight”;
这里的$1是怎么得来的??

还有下面这个的$1、$2为什么这么匹配??
$_=“green scaly dinosaur”;
s/(\w+)(\w+)/$2,$1/;#现在为“scaly,green dinosaur”;


分少请见谅!!

[解决办法]
$1-$9是正则表达式中使用的变量,用来提取正则中匹配到的项


$_是默认的当然变量
比如对一个循环来说,默认的当前变量就是循环变量
一个操作如果没有指定操作的变量,就是对默认变量的操作
s/with(\w+)/agaist$1’s team/;
就是这个意思
[解决办法]
嗯,也叫 捕获变量

热点排行