• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

[转-改]Perl初级教程(四)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

字符串匹配

  则表达式-regular expression

   规则表达式包含在斜线内,匹配通过=~操作符进行。如果字符串the出现在变量$sentence中,则下面的表达式为真:

    $sentence =~ /the/

  RE是大小写敏感的,所以如果

  $sentence = "The quick brown fox";

  那么上面的匹配结果为false。

   


$TestStr = "Contains a test !" ;
if ($TestStr = ! ~/test/)
{
    
print("Contain ! ");
}
else
{
    
print("No contain !");
}
> No contain !

 

下面是一些特殊的RE字符和它们的意义:

.	# Any single character except a newline
^	# The beginning of the line or string
$	# The end of the line or string
*	# Zero or more of the last character
+	# One or more of the last character
?	# Zero or one of the last character

 

 【特殊变量[URL]

   1. $$

   perl -e "sleep(5);print qq(The PID of process perl.exe is : $$)";  # 命令行运行, $$是当前perl解析器的进程ID(即,PID).

> The PID of process perl.exe is : 1871

   2. $_.

  

@FruitList = ("Apple ","Pear ","Orange ","Peach ");

foreach(@FruitList)
{
print $_."\n";
}
> Apple
  Pear
  Orange
  Peach
 

在程序第五行中:foreach (@array) 实际上就是等于 foreach $_ (@array),在这里,$_是默认是输入/输出,因此,如果在程序中有类似的没有明确声明的变量位置的时候,就有可能能使用$_代替。

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
perl 交叉编译发布时间:2022-07-22
下一篇:
perl打造自己的黑客工具[转]发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap