Perl正则表达式

  • A+
所属分类:成长之路

正则表达式描述了一种字符串匹配的模式,可用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。

Perl语言的正则表达式功能非常的强大,基本是常用语言中最强大的,很多语言设计正则式支持的时候都参考Perl的正则表达式。

Perl的正则表达式的三种形式:

  • 匹配:m//(还可简写为//,省略掉m)
  • 替换:s///
  • 转化:tr///

这三种形式一般都和=~或!~搭配使用,=~表示相匹配,!~表示不匹配。

匹配操作

匹配操作符m//用于匹配一个字符串语句或者一个正则表达式,例如,需要匹配标量$birdteam中的net,代码如下所示:

  1. #!/usr/bin/perl
  2. $birdteam = "Welcome to birdteam.net!";
  3. if ($birdteam =~ /net/){
  4.    print "第一次匹配上\n";
  5. }else{
  6.    print "第一次不匹配\n";
  7. }
  8. $birdteam = "net";
  9. if ($birdteam =~ /net/){
  10.    print "第二次匹配上\n";
  11. }else{
  12.    print "第二次不匹配\n";
  13. }

执行以上程序,输出的结果为:

第一次匹配上

第二次匹配上

模式匹配修饰

模式匹配有一些常用修饰符,下表所示:

[table id=23 /]

正则表达式的变量

perl处理完会给匹配到的值存在三个特殊变量名:

  • $`:匹配部分的前一部分字符串;
  • $&:匹配的字符串;
  • $':还没匹配的剩余字符串。

如果将这三个变量放在一起,将得到原始字符串。

实例如下:

  1. #!/usr/bin/perl
  2. $birdteam = "Welcome to birdteam.net!";
  3. if ($birdteam =~ /net/){
  4.    print "第一次匹配上\n";
  5. }else{
  6.    print "第一次不匹配\n";
  7. }
  8. $birdteam = "net";
  9. if ($birdteam =~ /net/){
  10.    print "第二次匹配上\n";
  11. }else{
  12.    print "第二次不匹配\n";
  13. }

执行上面的程序输出结果为:

匹配前字符串: welcome to birdteam.

匹配的字符串: net

匹配后字符串: !

替换操作

替换操作符s///是匹配操作的扩展,使用新字符串替换指定的字符串。基本格式如下:

s/PATTERN/REPLACEMENT/;

PATTERN为匹配模式,REPLACEMENT为替换的字符串。

例如我们将以下字符串的birdteam替换为net:

  1. #!/usr/bin/perl
  2. $string = "Welcome to birdteam.";
  3. $string =~ s/birdteam/net/;
  4. print "$string\n";

执行上面的程序输出结果为:

Welcome to net.

替换操作修饰

替换操作修饰符如下表所示:

[table id=24 /]

转化操作

以下是转化操作符相关的修饰符:

[table id=25 /]

以下实例将变量$string中所有小写字母转化为大写字母:

  1. #!/usr/bin/perl 
  2. $string = 'welcome to birdteam.net!';
  3. $string =~ tr/a-z/A-z/;
  4. print "$string\n";

执行上面的程序输出结果为:

WELCOME TO BIRDTEAM.NET!

以下实例使用/s将变量$string重复的字符删除:

  1. #!/usr/bin/perl
  2. $string = 'hello birdteam';
  3. $string =~ tr/a-z/a-z/s;
  4. print "$string\n";

执行上面的程序输出结果为:

helo birdteam

更多实例:

  1. $string =~ tr/\d/ /c;    #把所有非数字的字符替换为空格
  2. $string =~ tr/\t //d;    #删除tab和空格
  3. $string =~ tr/0-9/ /cs    #把数字间的其它字符替换为一个空格
  • 问答中心
  • 微信小程序扫一扫
  • weinxin
  • 微信公众
  • 微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:27   其中:访客  27   博主  0

    • avatar grolyrtolemcs

      Can I simply say what a relief to seek out someone who truly is aware of what theyre talking about on the internet. You undoubtedly know find out how to deliver a difficulty to mild and make it important. Extra people need to read this and perceive this side of the story. I cant imagine youre not more standard because you positively have the gift.

      • avatar tinyurl.com

        Hello there! This post could not be written much better!
        Looking at this article reminds me of my previous roommate!
        He continually kept preaching about this. I’ll forward this article to him.
        Pretty sure he’ll have a good read. I appreciate you for sharing!

        • avatar www.webverzeichnis-owl.de

          Normally I don’t read article on blogs, but I would like
          to say that this write-up very compelled me to take a look at and do so!
          Your writing taste has been surprised me. Thank you,
          quite nice post.

          • avatar http://t.co/4kPhN7Ek0L

            Cbd oil that works 2020
            I’m curious to find out what blog system you’re working with?
            I’m having some small security issues with my latest blog and I would like to
            find something more risk-free. Do you have any
            suggestions? best rated cbd oil http://t.co/4kPhN7Ek0L cbd
            oil that works 2020 http://t.co/4kPhN7Ek0L

            • avatar g https://tinyurl.com/rsacwgxy

              Why viewers still use to read news papers when in this technological world the whole thing
              is existing on net?

              • avatar svolrfinotews

                I know this if off topic but I’m looking into starting my own blog and was wondering what all is required to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web savvy so I’m not 100 sure. Any tips or advice would be greatly appreciated. Many thanks

                • avatar svolrfinotews

                  Hello my family member! I wish to say that this post is amazing, great written and include approximately all important infos. I?¦d like to see extra posts like this .