加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl正则表达式

发布时间:2020-12-15 23:46:46 所属栏目:大数据 来源:网络整理
导读: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 {} repeti

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

To present a metacharacter as a data character standing for itself,precede it with??(e.g.?.?matches the full stop character?.?only).


Repetition

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?escape
21??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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读