加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Learning Perl: 8.9. A Pattern Test Program

发布时间:2020-12-15 20:55:47 所属栏目:大数据 来源:网络整理
导读:? 8.9. A Pattern Test Program When in the course of Perl events it becomes necessary for a programmer to write a regular expression,it may be difficult to tell what the pattern will do. It's normal to find that a pattern matches more than

Previous Page

Next Page

?

8.9. A Pattern Test Program

When in the course of Perl events it becomes necessary for a programmer to write a regular expression,it may be difficult to tell what the pattern will do. It's normal to find that a pattern matches more than you expected,or less. Or it may match earlier in the string than you expected,or later,or not at all.

This program is useful to test out a pattern on some strings and see what it matches and where:

    #!/usr/bin/perl
    while (<>) {                   # take one input line at a time
      chomp;
      if (/YOUR_PATTERN_GOES_HERE/) {
        print "Matched: |$`<$&>$'|/n";  # the special match vars
      } else {
        print "No match: |$_|/n";
      }
    }

This pattern test program is written for programmers and not end users to use,you can tell because it doesn't have any prompts or usage information. It will take any number of input lines and check each one against the pattern that you'll put in place of the string saying YOUR_PATTERN_GOES_HERE. For each line that matches,it uses the three special match variables ($`,$&,and $') to make a picture of where the match happened. What you'll see is this: if the pattern is /match/ and the input is beforematchafter,the output will say "|before<match>after|",using angle brackets to show you what part of the string was matched by your pattern. If your pattern matches something you didn't expect,you'll see it right away.

Previous Page

Next Page

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读