perl Pattern Matching
时间:2007-06-25 来源:ilovit
Modifier | Meaning |
---|---|
i | Do case-insensitive pattern matching. |
m |
Treat string as multiple lines (^ and $ match internal \n). |
s |
Treat string as single line (^ and $ ignore \n, but . matches \n). |
x |
Extend your pattern's legibility with whitespace and comments. |
Assertion | Meaning |
---|---|
^ |
Matches at the beginning of the string (or line, if /m used) |
$ |
Matches at the end of the string (or line, if /m used) |
\b |
Matches at word boundary (between \w and \W) |
\B |
Matches except at word boundary |
\A |
Matches at the beginning of the string |
\Z |
Matches at the end of the string |
\G |
Matches where previous m//g left off |
(?=...) |
Matches if engine would match ... next |
(?!...) |
Matches if engine wouldn't match ... next |
Maximal | Minimal | Allowed Range |
---|---|---|
{n,m} | {n,m}? |
Must occur at least n times but no more than m times |
{n,} | {n,}? |
Must occur at least n times |
{n} | {n}? |
Must match exactly n times |
* | *? |
0 or more times (same as {0,}) |
+ | +? |
1 or more times (same as {1,}) |
? | ?? |
0 or 1 time (same as {0,1}) |
Code | Matches |
---|---|
\a | Alarm (beep) |
\n | Newline |
\r | Carriage return |
\t | Tab |
\f | Formfeed |
\e | Escape |
\d | A digit, same as [0-9] |
\D | A nondigit |
\w | A word character (alphanumeric), same as [a-zA-Z_0-9] |
\W | A nonword character |
\s | A whitespace character, same as [ \t\n\r\f] |
\S | A non-whitespace character |
Modifier | Meaning |
---|---|
g | Match globally, that is, find all occurrences. |
i | Do case-insensitive pattern matching. |
m | Treat string as multiple lines. (continued) |
o | Only compile pattern once. |
s | Treat string as single line. |
x | Use extended regular expressions. |
Modifier | Meaning |
---|---|
e | Evaluate the right side as an expression. |
g | Replace globally, that is, all occurrences. |
i | Do case-insensitive pattern matching. |
m | Treat string as multiple lines. |
o | Only compile pattern once. |
s | Treat string as single line. |
x | Use extended regular expressions. |
Modifier | Meaning |
---|---|
c | Complement the SEARCHLIST. |
d | Delete found but unreplaced characters. |
s | Squash duplicate replaced characters. |