Learning Perl: 8.9. A Pattern Test Program
来源:互联网 发布:淘宝买运费保险可靠吗 编辑:程序博客网 时间:2024/05/14 02:34
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.
- Learning Perl: 8.9. A Pattern Test Program
- Learning Perl: 1.4. How Do I Make a Perl Program?
- a test program for polymophism in c++
- A Linux serial port test program
- A Linux serial port test program
- The Perl Tutorial: Running a Perl Program (2)
- Learning Perl: 4.1. Defining a Subroutine
- Learning Perl: 4.2. Invoking a Subroutine
- Learning Perl: 5.7. Opening a Filehandle
- Perl Learning: 5.10. Reopening a Standard Filehandle
- Learning Perl: 6.1. What Is a Hash?
- Learning Perl: 1.5. A Whirlwind Tour of Perl
- Learning Perl
- Learning Perl
- Learning Perl
- Learning Perl:
- Learning Perl
- Learning perl
- Learning Perl: 8.7. General Quantifiers
- CSS Specificity--CSS特性、权重、优先级---CSS specificity规则、算法及实例分析
- 考研必备
- Learning Perl: 8.8. Precedence
- 生活!
- Learning Perl: 8.9. A Pattern Test Program
- Android 之 Activity 生命周期
- QThread的用法!qt多线程编程
- Learning Perl: 9.1. Substitutions with s///
- C#字符串处理函数整理之substring
- C/C++中关于地址、指针和引用变量的学习笔记(十) : 引用型变量
- Learning Perl: 9.2. The split Operator
- PHP中双引号和单引号的区别和用法
- 10.10 信号_alarm和pause函数