^ | 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 |
To present a Metacharacter as a data character standing for itself,precede it with \
(e.g. \.
matches the full stop character .
only).
a* | zero or more a’s | ||
+ | one or more ? | zero or one ’s (i.e.,optional ) | |
{m} | exactly m ,} | at least m} | but at most nrepetitionsame as repetition but theshortest match is taken |
Read the notation a’s as “occurrences of strings,each of which matches the pattern ”. Read repetition as any of the repetition Expressions Listed above it. Shortest match means that the shortest string matching the pattern is taken. The default is “greedy matching”,which finds the longest match. Therepetition?
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 escape1 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..$N Tagged Expressions used in substitutions总结
以上是内存溢出为你收集整理的Perl正则表达式全部内容,希望文章能够帮你解决Perl正则表达式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)