Perl中如何使用正则表达式

Perl中如何使用正则表达式

Perl中如何使用正则表达式,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

Perl正则表达式

更多的时候,我们可能不能使用默认变量来进行Perl正则表达式的匹配,所以Perl提供了一个专门的运算符“=~”,它专门用来进行Perl正则表达式的匹配。忽略大小写

#!/usr/bin/Perl-w  $str="helloPerlworld!\n";  if($str=~/Perl/i){###忽略大小写,匹配$str中是否存在Perl字符;  print"$str";  }

Perl中的Perl正则表达式还可以使用变量来动态地改变匹配的模式,如:

1、字符串匹配

[root@test-linuxtmp]#catp.pl  #!/usr/bin/Perl-w  $str="helloPerlprogram!\n";  print"youinputis:\n";  $input=<STDIN>;  chomp($input);  if($str=~/$input/i){  print"haha,find$str\n";  }  [root@test-linuxtmp]#./p.pl  youinputis:  hel  haha,findhelloPerlprogram!  [root@test-linuxtmp]#

2、字符串替换

[root@test-linuxtmp]#./pap.pl  oldstringis:helloPerlWorld!  newstringis:helloPerlNewWorld!  [root@test-linuxtmp]#  [root@test-linuxtmp]#catpap.pl  #!/usr/bin/Perl-w  $_="helloPerlWorld!\n";  print"oldstringis:$_";  s/Perl/PerlNew/g;  print"newstringis:$_";

关于Perl中如何使用正则表达式问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。