Perl正则表达式
2015-03-16 18:28
176 查看
Metacharacters
^ | beginning of string |
$ | end of string |
. | any character except newline |
* | match 0 or more times |
+ | match 1 or more times |
? | match 0 or 1 times; or shortest match |
| | alternative |
() | grouping; "storing" |
[] | set of characters |
{} | repetition modifier |
\ | quote or special |
\(e.g.
\.matches the full stop character
.only).
Repetition
a* | zero or more a’s |
a+ | one or more a’s |
a? | zero or one a’s (i.e., optional a) |
a{m } | exactly m a’s |
a{m ,} | at least m a’s |
a{m ,m} | at least m but at most na’s |
repetition? | same as repetition but theshortest match is taken |
?construct was introduced in Perl version 5.
Calling these "wildcards" may actually conflict with the theoretical grammar
and syntax of Perl, but in fact is the most intuitive way to think of it,
and will not lead to any coding mistakes.
. Match any character \w Match "word" character (alphanumeric plus "_") \W Match non-word character \s Match whitespace character \S Match non-whitespace character \d Match digit character \D Match non-digit character \t Match tab \n Match newline \r Match return \f Match formfeed \a Match alarm (bell, beep, etc) \e Match escape \021 Match octal char ( in this case 21 octal) \xf0 Match hex char ( in this case f0 hexidecimal)You can follow any character, wildcard, or series of characters and/or wildcard
with a repetiton. Here's where you start getting some power:
* Match 0 or more times + Match 1 or more times ? Match 1 or 0 times {n} Match exactly n times {n,} Match at least n times {n,m} Match at least n but not more than m times
Code Meaning ---------- -------------------------------------------- \w Alphanumeric Characters \W Non-Alphanumeric Characters \s White Space \S Non-White Space \d Digits \D Non-Digits \b Word Boundary \B Non-Word Boundary \A or ^ At the beginning of a string \Z or $ At the end of a string . Match Any single character * Zero or more occurrences ? Zero or one Occurences + one or more occurences {N} Exactly N occurences {N,M} Between N and M occurences .*<thingy> Greedy Match, up to the last thingy .*?<thingy> Non-Greedy match, up to the first thingy [set_of_things] Match any item in the set [^set_of_things] Does not match anything in the set (some_expression) Tag an expression $1..$N Tagged expressions used in substitutions
相关文章推荐
- perl 正则表达式使用技巧
- Perl 中的正则表达式
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(7): Split 函数
- perl 正则表达式介绍
- posix和perl标准的正则表达式区别
- 学习perl正则表达式
- 解析posix与perl标准的正则表达式区别
- [收藏] 正则表达式的入门与应用,1~4篇!grep/sed/perl/awk
- 正则表达式详解(Perl)
- grep、sed、awk、perl等对正则表达式的支持的差别
- Perl正则表达式例子
- perl的正则表达式中的元字符(需要转义)
- perl入门教程:正则表达式
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(4): Replace
- Perl 正则表达式
- Perl正则表达式
- Perl中的正则表达式
- perl学习之正则表达式
- perl 中部分正则表达式的标记