📄 perlretut.1
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "PERLRETUT 1".TH PERLRETUT 1 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"perlretut \- Perl regular expressions tutorial.SH "DESCRIPTION".IX Header "DESCRIPTION"This page provides a basic tutorial on understanding, creating andusing regular expressions in Perl. It serves as a complement to thereference page on regular expressions perlre. Regular expressionsare an integral part of the \f(CW\*(C`m//\*(C'\fR, \f(CW\*(C`s///\*(C'\fR, \f(CW\*(C`qr//\*(C'\fR and \f(CW\*(C`split\*(C'\fRoperators and so this tutorial also overlaps with\&\*(L"Regexp Quote-Like Operators\*(R" in perlop and \*(L"split\*(R" in perlfunc..PPPerl is widely renowned for excellence in text processing, and regularexpressions are one of the big factors behind this fame. Perl regularexpressions display an efficiency and flexibility unknown in mostother computer languages. Mastering even the basics of regularexpressions will allow you to manipulate text with surprising ease..PPWhat is a regular expression? A regular expression is simply a stringthat describes a pattern. Patterns are in common use these days;examples are the patterns typed into a search engine to find web pagesand the patterns used to list files in a directory, e.g., \f(CW\*(C`ls *.txt\*(C'\fRor \f(CW\*(C`dir *.*\*(C'\fR. In Perl, the patterns described by regular expressionsare used to search strings, extract desired parts of strings, and todo search and replace operations..PPRegular expressions have the undeserved reputation of being abstractand difficult to understand. Regular expressions are constructed usingsimple concepts like conditionals and loops and are no more difficultto understand than the corresponding \f(CW\*(C`if\*(C'\fR conditionals and \f(CW\*(C`while\*(C'\fRloops in the Perl language itself. In fact, the main challenge inlearning regular expressions is just getting used to the tersenotation used to express these concepts..PPThis tutorial flattens the learning curve by discussing regularexpression concepts, along with their notation, one at a time and withmany examples. The first part of the tutorial will progress from thesimplest word searches to the basic regular expression concepts. Ifyou master the first part, you will have all the tools needed to solveabout 98% of your needs. The second part of the tutorial is for thosecomfortable with the basics and hungry for more power tools. Itdiscusses the more advanced regular expression operators andintroduces the latest cutting edge innovations in 5.6.0..PPA note: to save time, 'regular expression' is often abbreviated asregexp or regex. Regexp is a more natural abbreviation than regex, butis harder to pronounce. The Perl pod documentation is evenly split onregexp vs regex; in Perl, there is more than one way to abbreviate it.We'll use regexp in this tutorial..SH "Part 1: The basics".IX Header "Part 1: The basics".Sh "Simple word matching".IX Subsection "Simple word matching"The simplest regexp is simply a word, or more generally, a string ofcharacters. A regexp consisting of a word matches any string thatcontains that word:.PP.Vb 1\& "Hello World" =~ /World/; # matches.Ve.PPWhat is this Perl statement all about? \f(CW"Hello World"\fR is a simpledouble quoted string. \f(CW\*(C`World\*(C'\fR is the regular expression and the\&\f(CW\*(C`//\*(C'\fR enclosing \f(CW\*(C`/World/\*(C'\fR tells Perl to search a string for a match.The operator \f(CW\*(C`=~\*(C'\fR associates the string with the regexp match andproduces a true value if the regexp matched, or false if the regexpdid not match. In our case, \f(CW\*(C`World\*(C'\fR matches the second word in\&\f(CW"Hello World"\fR, so the expression is true. Expressions like thisare useful in conditionals:.PP.Vb 6\& if ("Hello World" =~ /World/) {\& print "It matches\en";\& }\& else {\& print "It doesn\*(Aqt match\en";\& }.Ve.PPThere are useful variations on this theme. The sense of the match canbe reversed by using the \f(CW\*(C`!~\*(C'\fR operator:.PP.Vb 6\& if ("Hello World" !~ /World/) {\& print "It doesn\*(Aqt match\en";\& }\& else {\& print "It matches\en";\& }.Ve.PPThe literal string in the regexp can be replaced by a variable:.PP.Vb 7\& $greeting = "World";\& if ("Hello World" =~ /$greeting/) {\& print "It matches\en";\& }\& else {\& print "It doesn\*(Aqt match\en";\& }.Ve.PPIf you're matching against the special default variable \f(CW$_\fR, the\&\f(CW\*(C`$_ =~\*(C'\fR part can be omitted:.PP.Vb 7\& $_ = "Hello World";\& if (/World/) {\& print "It matches\en";\& }\& else {\& print "It doesn\*(Aqt match\en";\& }.Ve.PPAnd finally, the \f(CW\*(C`//\*(C'\fR default delimiters for a match can be changedto arbitrary delimiters by putting an \f(CW\*(Aqm\*(Aq\fR out front:.PP.Vb 4\& "Hello World" =~ m!World!; # matches, delimited by \*(Aq!\*(Aq\& "Hello World" =~ m{World}; # matches, note the matching \*(Aq{}\*(Aq\& "/usr/bin/perl" =~ m"/perl"; # matches after \*(Aq/usr/bin\*(Aq,\& # \*(Aq/\*(Aq becomes an ordinary char.Ve.PP\&\f(CW\*(C`/World/\*(C'\fR, \f(CW\*(C`m!World!\*(C'\fR, and \f(CW\*(C`m{World}\*(C'\fR all represent thesame thing. When, e.g., the quote (\f(CW\*(C`"\*(C'\fR) is used as a delimiter, the forwardslash \f(CW\*(Aq/\*(Aq\fR becomes an ordinary character and can be used in this regexpwithout trouble..PPLet's consider how different regexps would match \f(CW"Hello World"\fR:.PP.Vb 4\& "Hello World" =~ /world/; # doesn\*(Aqt match\& "Hello World" =~ /o W/; # matches\& "Hello World" =~ /oW/; # doesn\*(Aqt match\& "Hello World" =~ /World /; # doesn\*(Aqt match.Ve.PPThe first regexp \f(CW\*(C`world\*(C'\fR doesn't match because regexps arecase-sensitive. The second regexp matches because the substring\&\f(CW\*(Aqo\ W\*(Aq\fR occurs in the string \f(CW"Hello\ World"\fR. The spacecharacter ' ' is treated like any other character in a regexp and isneeded to match in this case. The lack of a space character is thereason the third regexp \f(CW\*(AqoW\*(Aq\fR doesn't match. The fourth regexp\&\f(CW\*(AqWorld \*(Aq\fR doesn't match because there is a space at the end of theregexp, but not at the end of the string. The lesson here is thatregexps must match a part of the string \fIexactly\fR in order for thestatement to be true..PPIf a regexp matches in more than one place in the string, Perl willalways match at the earliest possible point in the string:.PP.Vb 2\& "Hello World" =~ /o/; # matches \*(Aqo\*(Aq in \*(AqHello\*(Aq
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -