第七章 正则模式
发布时间:2020-12-14 01:24:58 所属栏目:百科 来源:网络整理
导读:模式分组:在数学中,圆括号() 用来分组。因此,圆括号也是元字符模式/fred+/ 会匹配像fredddddd这样的字符窜/(fred)+/ 会匹配像fredfredfred这种字符窜[root@jhoa 2015]# cat a8.pl $_="abba";if (/(.)1/){ ##匹配bb print "It matched same character next t
模式分组: 在数学中,圆括号() 用来分组。因此,圆括号也是元字符 模式/fred+/ 会匹配像fredddddd这样的字符窜 /(fred)+/ 会匹配像fredfredfred这种字符窜 [root@jhoa 2015]# cat a8.pl $_="abba"; if (/(.)1/){ ##匹配bb print "It matched same character next to itself!n"; print "$1 is $1n"; } [root@jhoa 2015]# perl a8.pl It matched same character next to itself! $1 is b (.)1 表明需要匹配连续出现的两个同样的字符 反向引用不必总是附在相应的括号后面,下面的模式会匹配y后面的4个连续的非回车字符,并且用1在d字符之后重复这个4个字符 [root@jhoa 2015]# cat a9.pl $_ = "yabba dabba doo"; if (/y(....) d1/) { print "It matched the same after y and d !n"; print "$1 is $1n"; } [root@jhoa 2015]# perl a9.pl It matched the same after y and d ! $1 is abba [root@jhoa 2015]# cat a11.pl $_ = "yabba dabba doo"; if (/y(.)(.)21/) { print "It matched the same after y and d !n"; print "$1 is $1n"; print "$2 is $2n"; } [root@jhoa 2015]# perl a11.pl It matched the same after y and d ! $1 is a $2 is b 匹配的是abba $_ = "yabba dabba doo"; if (/y((.)(.)32) d1/) { print "It matched the same after y and d !n"; print "$1 is $1n"; print "$2 is $2n"; } ~ ~ ( /y( (.)(.)32 ) d1/ ) ~ [root@jhoa 2015]# cat a10.pl $_ = "yabba dabba doo"; if (/y((.)(.)32) d1/) { print "It matched the same after y and d !n"; print "$1 is $1n"; print "$2 is $2n"; print "$3 is $3n"; } [root@jhoa 2015]# perl a10.pl It matched the same after y and d ! $1 is abba $2 is a $3 is b 匹配abbd dabbd d (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |