@H_404_23@
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 @R_190_4045@ion. 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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。