#!/usr/bin/perl -w
use strict;
=head
this section describes regular expression
=cut
=head1
1 operators
match: =~
not match: !~
2 types for use
match: m/pattern/ or /pattern/
replace: s/pattern/replacement/
transform: tr/pattern/replacement/
3 patterns
. match all chars except "/n";
x? count of x = 0 or 1;
x* count of x >= 0;
x+ count of x >=1;
.* match any char whose count >= 0;
.+ match any char whose count >= 1;
x{m} match x whose count = m;
x{m,n} match x whose count is in [m, n];
x{m,} match x whose count >= m;
x{,n} match x whose count <= n;
[] match any char present in [];
[^] match any char NOT in [], ^ ONLY affect when at the beginning of pattern;
[0-9] match one digital char;
[^0-9] match one char except digital char;
/d equal to [0-9];
/d+ equal to [0-9]+;
/D match one non-digital char;
/D+ match one or more than one non-digital char;
/w match one char in [a-zA-Z_0-9];
/w+ equal to [a-zA-Z_0-9]+;
/W equal to [^a-zA-Z_0-9];
/W+ equal to [^a-zA-Z_0-9]+;
/s match char in [/n/t/r/f], space char;
/s+ [/n/t/r/f]+;
/S [^/n/t/r/f];
/S+ [^/n/t/r/f]+;
/b ;
/B ;
/^pattern/ match very beginning chars;
/pattern$/ match very end chars;
4
/pattern/i: ignore if the pattern is in upper or lower;
/pattern/s: match pattern during multi-lines, ignore "/n";
s/pattern/replacement/g: replace all strings matching pattern;
s/pattern/replacement/e: treat replacement as a expression, not a string;
5 match results
m/(p1)(p2)../:
use (pattern), strings which are matched the patterns in () can be represented
in the way: $1, $2,..., which $1 represent the first string matched the p1;
m/((p1)(p2))/: $1 is the string matched the most left ();
$1 is always stored in memory unless the next match is successful; so if the
current match failed, $1 stored the value matched by last time;
6 exchange for strings
=cut
294

被折叠的 条评论
为什么被折叠?



