⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perlrequick.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 2 页
字号:
.Sp.Vb 1\&    [0\-9a\-zA\-Z_].Ve.IP "\(bu" 4\&\eD is a negated \ed; it represents any character but a digit.Sp.Vb 1\&    [^0\-9].Ve.IP "\(bu" 4\&\eS is a negated \es; it represents any non-whitespace character.Sp.Vb 1\&    [^\es].Ve.IP "\(bu" 4\&\eW is a negated \ew; it represents any non-word character.Sp.Vb 1\&    [^\ew].Ve.IP "\(bu" 4The period '.' matches any character but \*(L"\en\*(R".PPThe \f(CW\*(C`\ed\es\ew\eD\eS\eW\*(C'\fR abbreviations can be used both inside and outsideof character classes.  Here are some in use:.PP.Vb 7\&    /\ed\ed:\ed\ed:\ed\ed/; # matches a hh:mm:ss time format\&    /[\ed\es]/;         # matches any digit or whitespace character\&    /\ew\eW\ew/;         # matches a word char, followed by a\&                      # non\-word char, followed by a word char\&    /..rt/;           # matches any two chars, followed by \*(Aqrt\*(Aq\&    /end\e./;          # matches \*(Aqend.\*(Aq\&    /end[.]/;         # same thing, matches \*(Aqend.\*(Aq.Ve.PPThe \fBword\ anchor\fR\  \f(CW\*(C`\eb\*(C'\fR matches a boundary between a wordcharacter and a non-word character \f(CW\*(C`\ew\eW\*(C'\fR or \f(CW\*(C`\eW\ew\*(C'\fR:.PP.Vb 4\&    $x = "Housecat catenates house and cat";\&    $x =~ /\ebcat/;  # matches cat in \*(Aqcatenates\*(Aq\&    $x =~ /cat\eb/;  # matches cat in \*(Aqhousecat\*(Aq\&    $x =~ /\ebcat\eb/;  # matches \*(Aqcat\*(Aq at end of string.Ve.PPIn the last example, the end of the string is considered a wordboundary..Sh "Matching this or that".IX Subsection "Matching this or that"We can match different character strings with the \fBalternation\fRmetacharacter \f(CW\*(Aq|\*(Aq\fR.  To match \f(CW\*(C`dog\*(C'\fR or \f(CW\*(C`cat\*(C'\fR, we form the regex\&\f(CW\*(C`dog|cat\*(C'\fR.  As before, perl will try to match the regex at theearliest possible point in the string.  At each character position,perl will first try to match the first alternative, \f(CW\*(C`dog\*(C'\fR.  If\&\f(CW\*(C`dog\*(C'\fR doesn't match, perl will then try the next alternative, \f(CW\*(C`cat\*(C'\fR.If \f(CW\*(C`cat\*(C'\fR doesn't match either, then the match fails and perl moves tothe next position in the string.  Some examples:.PP.Vb 2\&    "cats and dogs" =~ /cat|dog|bird/;  # matches "cat"\&    "cats and dogs" =~ /dog|cat|bird/;  # matches "cat".Ve.PPEven though \f(CW\*(C`dog\*(C'\fR is the first alternative in the second regex,\&\f(CW\*(C`cat\*(C'\fR is able to match earlier in the string..PP.Vb 2\&    "cats"          =~ /c|ca|cat|cats/; # matches "c"\&    "cats"          =~ /cats|cat|ca|c/; # matches "cats".Ve.PPAt a given character position, the first alternative that allows theregex match to succeed will be the one that matches. Here, all thealternatives match at the first string position, so the first matches..Sh "Grouping things and hierarchical matching".IX Subsection "Grouping things and hierarchical matching"The \fBgrouping\fR metacharacters \f(CW\*(C`()\*(C'\fR allow a part of a regex to betreated as a single unit.  Parts of a regex are grouped by enclosingthem in parentheses.  The regex \f(CW\*(C`house(cat|keeper)\*(C'\fR means match\&\f(CW\*(C`house\*(C'\fR followed by either \f(CW\*(C`cat\*(C'\fR or \f(CW\*(C`keeper\*(C'\fR.  Some more examplesare.PP.Vb 2\&    /(a|b)b/;    # matches \*(Aqab\*(Aq or \*(Aqbb\*(Aq\&    /(^a|b)c/;   # matches \*(Aqac\*(Aq at start of string or \*(Aqbc\*(Aq anywhere\&\&    /house(cat|)/;  # matches either \*(Aqhousecat\*(Aq or \*(Aqhouse\*(Aq\&    /house(cat(s|)|)/;  # matches either \*(Aqhousecats\*(Aq or \*(Aqhousecat\*(Aq or\&                        # \*(Aqhouse\*(Aq.  Note groups can be nested.\&\&    "20" =~ /(19|20|)\ed\ed/;  # matches the null alternative \*(Aq()\ed\ed\*(Aq,\&                             # because \*(Aq20\ed\ed\*(Aq can\*(Aqt match.Ve.Sh "Extracting matches".IX Subsection "Extracting matches"The grouping metacharacters \f(CW\*(C`()\*(C'\fR also allow the extraction of theparts of a string that matched.  For each grouping, the part thatmatched inside goes into the special variables \f(CW$1\fR, \f(CW$2\fR, etc.They can be used just as ordinary variables:.PP.Vb 5\&    # extract hours, minutes, seconds\&    $time =~ /(\ed\ed):(\ed\ed):(\ed\ed)/;  # match hh:mm:ss format\&    $hours = $1;\&    $minutes = $2;\&    $seconds = $3;.Ve.PPIn list context, a match \f(CW\*(C`/regex/\*(C'\fR with groupings will return thelist of matched values \f(CW\*(C`($1,$2,...)\*(C'\fR.  So we could rewrite it as.PP.Vb 1\&    ($hours, $minutes, $second) = ($time =~ /(\ed\ed):(\ed\ed):(\ed\ed)/);.Ve.PPIf the groupings in a regex are nested, \f(CW$1\fR gets the group with theleftmost opening parenthesis, \f(CW$2\fR the next opening parenthesis,etc.  For example, here is a complex regex and the matching variablesindicated below it:.PP.Vb 2\&    /(ab(cd|ef)((gi)|j))/;\&     1  2      34.Ve.PPAssociated with the matching variables \f(CW$1\fR, \f(CW$2\fR, ... arethe \fBbackreferences\fR \f(CW\*(C`\e1\*(C'\fR, \f(CW\*(C`\e2\*(C'\fR, ...  Backreferences arematching variables that can be used \fIinside\fR a regex:.PP.Vb 1\&    /(\ew\ew\ew)\es\e1/; # find sequences like \*(Aqthe the\*(Aq in string.Ve.PP\&\f(CW$1\fR, \f(CW$2\fR, ... should only be used outside of a regex, and \f(CW\*(C`\e1\*(C'\fR,\&\f(CW\*(C`\e2\*(C'\fR, ... only inside a regex..Sh "Matching repetitions".IX Subsection "Matching repetitions"The \fBquantifier\fR metacharacters \f(CW\*(C`?\*(C'\fR, \f(CW\*(C`*\*(C'\fR, \f(CW\*(C`+\*(C'\fR, and \f(CW\*(C`{}\*(C'\fR allow usto determine the number of repeats of a portion of a regex weconsider to be a match.  Quantifiers are put immediately after thecharacter, character class, or grouping that we want to specify.  Theyhave the following meanings:.IP "\(bu" 4\&\f(CW\*(C`a?\*(C'\fR = match 'a' 1 or 0 times.IP "\(bu" 4\&\f(CW\*(C`a*\*(C'\fR = match 'a' 0 or more times, i.e., any number of times.IP "\(bu" 4\&\f(CW\*(C`a+\*(C'\fR = match 'a' 1 or more times, i.e., at least once.IP "\(bu" 4\&\f(CW\*(C`a{n,m}\*(C'\fR = match at least \f(CW\*(C`n\*(C'\fR times, but not more than \f(CW\*(C`m\*(C'\fRtimes..IP "\(bu" 4\&\f(CW\*(C`a{n,}\*(C'\fR = match at least \f(CW\*(C`n\*(C'\fR or more times.IP "\(bu" 4\&\f(CW\*(C`a{n}\*(C'\fR = match exactly \f(CW\*(C`n\*(C'\fR times.PPHere are some examples:.PP.Vb 6\&    /[a\-z]+\es+\ed*/;  # match a lowercase word, at least some space, and\&                     # any number of digits\&    /(\ew+)\es+\e1/;    # match doubled words of arbitrary length\&    $year =~ /\ed{2,4}/;  # make sure year is at least 2 but not more\&                         # than 4 digits\&    $year =~ /\ed{4}|\ed{2}/;    # better match; throw out 3 digit dates.Ve.PPThese quantifiers will try to match as much of the string as possible,while still allowing the regex to match.  So we have.PP.Vb 5\&    $x = \*(Aqthe cat in the hat\*(Aq;\&    $x =~ /^(.*)(at)(.*)$/; # matches,\&                            # $1 = \*(Aqthe cat in the h\*(Aq\&                            # $2 = \*(Aqat\*(Aq\&                            # $3 = \*(Aq\*(Aq   (0 matches).Ve.PPThe first quantifier \f(CW\*(C`.*\*(C'\fR grabs as much of the string as possiblewhile still having the regex match. The second quantifier \f(CW\*(C`.*\*(C'\fR hasno string left to it, so it matches 0 times..Sh "More matching".IX Subsection "More matching"There are a few more things you might want to know about matchingoperators.  In the code.PP.Vb 4\&    $pattern = \*(AqSeuss\*(Aq;\&    while (<>) {\&        print if /$pattern/;\&    }.Ve.PPperl has to re-evaluate \f(CW$pattern\fR each time through the loop.  If\&\f(CW$pattern\fR won't be changing, use the \f(CW\*(C`//o\*(C'\fR modifier, to onlyperform variable substitutions once.  If you don't want anysubstitutions at all, use the special delimiter \f(CW\*(C`m\*(Aq\*(Aq\*(C'\fR:.PP.Vb 3\&    @pattern = (\*(AqSeuss\*(Aq);\&    m/@pattern/; # matches \*(AqSeuss\*(Aq\&    m\*(Aq@pattern\*(Aq; # matches the literal string \*(Aq@pattern\*(Aq.Ve.PPThe global modifier \f(CW\*(C`//g\*(C'\fR allows the matching operator to matchwithin a string as many times as possible.  In scalar context,successive matches against a string will have \f(CW\*(C`//g\*(C'\fR jump from matchto match, keeping track of position in the string as it goes along.You can get or set the position with the \f(CW\*(C`pos()\*(C'\fR function.For example,.PP.Vb 4\&    $x = "cat dog house"; # 3 words\&    while ($x =~ /(\ew+)/g) {\&        print "Word is $1, ends at position ", pos $x, "\en";\&    }.Ve.PPprints.PP.Vb 3\&    Word is cat, ends at position 3\&    Word is dog, ends at position 7\&    Word is house, ends at position 13.Ve.PPA failed match or changing the target string resets the position.  Ifyou don't want the position reset after failure to match, add the\&\f(CW\*(C`//c\*(C'\fR, as in \f(CW\*(C`/regex/gc\*(C'\fR..PPIn list context, \f(CW\*(C`//g\*(C'\fR returns a list of matched groupings, or ifthere are no groupings, a list of matches to the whole regex.  So.PP.Vb 4\&    @words = ($x =~ /(\ew+)/g);  # matches,\&                                # $word[0] = \*(Aqcat\*(Aq\&                                # $word[1] = \*(Aqdog\*(Aq\&                                # $word[2] = \*(Aqhouse\*(Aq.Ve.Sh "Search and replace".IX Subsection "Search and replace"Search and replace is performed using \f(CW\*(C`s/regex/replacement/modifiers\*(C'\fR.The \f(CW\*(C`replacement\*(C'\fR is a Perl double quoted string that replaces in thestring whatever is matched with the \f(CW\*(C`regex\*(C'\fR.  The operator \f(CW\*(C`=~\*(C'\fR isalso used here to associate a string with \f(CW\*(C`s///\*(C'\fR.  If matchingagainst \f(CW$_\fR, the \f(CW\*(C`$_\ =~\*(C'\fR\  can be dropped.  If there is a match,\&\f(CW\*(C`s///\*(C'\fR returns the number of substitutions made, otherwise it returnsfalse.  Here are a few examples:.PP.Vb 5\&    $x = "Time to feed the cat!";\&    $x =~ s/cat/hacker/;   # $x contains "Time to feed the hacker!"\&    $y = "\*(Aqquoted words\*(Aq";\&    $y =~ s/^\*(Aq(.*)\*(Aq$/$1/;  # strip single quotes,\&                           # $y contains "quoted words".Ve.PPWith the \f(CW\*(C`s///\*(C'\fR operator, the matched variables \f(CW$1\fR, \f(CW$2\fR, etc.are immediately available for use in the replacement expression. Withthe global modifier, \f(CW\*(C`s///g\*(C'\fR will search and replace all occurrencesof the regex in the string:.PP.Vb 4\&    $x = "I batted 4 for 4";\&    $x =~ s/4/four/;   # $x contains "I batted four for 4"\&    $x = "I batted 4 for 4";\&    $x =~ s/4/four/g;  # $x contains "I batted four for four".Ve.PPThe evaluation modifier \f(CW\*(C`s///e\*(C'\fR wraps an \f(CW\*(C`eval{...}\*(C'\fR around thereplacement string and the evaluated result is substituted for thematched substring.  Some examples:.PP.Vb 3\&    # reverse all the words in a string\&    $x = "the cat in the hat";\&    $x =~ s/(\ew+)/reverse $1/ge;   # $x contains "eht tac ni eht tah"\&\&    # convert percentage to decimal\&    $x = "A 39% hit rate";\&    $x =~ s!(\ed+)%!$1/100!e;       # $x contains "A 0.39 hit rate".Ve.PPThe last example shows that \f(CW\*(C`s///\*(C'\fR can use other delimiters, such as\&\f(CW\*(C`s!!!\*(C'\fR and \f(CW\*(C`s{}{}\*(C'\fR, and even \f(CW\*(C`s{}//\*(C'\fR.  If single quotes are used\&\f(CW\*(C`s\*(Aq\*(Aq\*(Aq\*(C'\fR, then the regex and replacement are treated as single quotedstrings..Sh "The split operator".IX Subsection "The split operator"\&\f(CW\*(C`split /regex/, string\*(C'\fR splits \f(CW\*(C`string\*(C'\fR into a list of substringsand returns that list.  The regex determines the character sequencethat \f(CW\*(C`string\*(C'\fR is split with respect to.  For example, to split astring into words, use.PP.Vb 4\&    $x = "Calvin and Hobbes";\&    @word = split /\es+/, $x;  # $word[0] = \*(AqCalvin\*(Aq\&                              # $word[1] = \*(Aqand\*(Aq\&                              # $word[2] = \*(AqHobbes\*(Aq.Ve.PPTo extract a comma-delimited list of numbers, use.PP.Vb 4\&    $x = "1.618,2.718,   3.142";\&    @const = split /,\es*/, $x;  # $const[0] = \*(Aq1.618\*(Aq\&                                # $const[1] = \*(Aq2.718\*(Aq\&                                # $const[2] = \*(Aq3.142\*(Aq.Ve.PPIf the empty regex \f(CW\*(C`//\*(C'\fR is used, the string is split into individualcharacters.  If the regex has groupings, then the list produced containsthe matched substrings from the groupings as well:.PP.Vb 6\&    $x = "/usr/bin";\&    @parts = split m!(/)!, $x;  # $parts[0] = \*(Aq\*(Aq\&                                # $parts[1] = \*(Aq/\*(Aq\&                                # $parts[2] = \*(Aqusr\*(Aq\&                                # $parts[3] = \*(Aq/\*(Aq\&                                # $parts[4] = \*(Aqbin\*(Aq.Ve.PPSince the first character of \f(CW$x\fR matched the regex, \f(CW\*(C`split\*(C'\fR prependedan empty initial element to the list..SH "BUGS".IX Header "BUGS"None..SH "SEE ALSO".IX Header "SEE ALSO"This is just a quick start guide.  For a more in-depth tutorial onregexes, see perlretut and for the reference page, see perlre..SH "AUTHOR AND COPYRIGHT".IX Header "AUTHOR AND COPYRIGHT"Copyright (c) 2000 Mark KvaleAll rights reserved..PPThis document may be distributed under the same terms as Perl itself..Sh "Acknowledgments".IX Subsection "Acknowledgments"The author would like to thank Mark-Jason Dominus, Tom Christiansen,Ilya Zakharevich, Brad Hughes, and Mike Giroux for all their helpfulcomments.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -