最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

perl程序设计技巧

来源:懂视网 责编:小采 时间:2020-11-09 15:44:45
文档

perl程序设计技巧

perl程序设计技巧:1. Why does POE pass parameters as array slices http://poe.perl.org/POE_FAQ/calling_convention 2. perl regular expression fast referecnces metacharacters are {}[]() ^ $ . | * + a metacharacter can be matched by putting a backslash befo
推荐度:
导读perl程序设计技巧:1. Why does POE pass parameters as array slices http://poe.perl.org/POE_FAQ/calling_convention 2. perl regular expression fast referecnces metacharacters are {}[]() ^ $ . | * + a metacharacter can be matched by putting a backslash befo

1. Why does POE pass parameters as array slices? http://poe.perl.org/?POE_FAQ/calling_convention 2. perl regular expression fast referecnces metacharacters are {}[]() ^ $ . | * + ? a metacharacter can be matched by putting a backslash befo

1. Why does POE pass parameters as array slices?
http://poe.perl.org/?POE_FAQ/calling_convention

2. perl regular expression fast referecnces
metacharacters are

{ } [ ] ( ) ^ $ . | * + ?

a metacharacter can be matched by putting a backslash before it
anchor metacharacters ^ and $ .
The anchor ^ means match at the beginning of the string and the anchor $ means match at the end of the string, or before a newline at the end of the string.

"housekeeper" =~ /keeper/; # matches
"housekeeper" =~ /^keeper/; # doesn't match
"housekeeper" =~ /keeper$/; # matches
"housekeeper " =~ /keeper$/; # matches

A character class allows a set of possible characters, rather than just a single character, to match at a particular point in a regexp.Character classes are denoted by brackets [...], with the set of characters to be possiblly matched inside. Some examples:

/cat/; #matches 'cat'
/[bcr]at/; #matches 'bat', 'cat', or 'rat'
/item[0123456789]/; #matches 'item0' or ... or 'item9'
"abc" =~ /[cab]/; #matches 'a'
/[yY][eE][sS]/ can be rewritten as /yes/i.

The 'i' stands for case-insensitive and is an example of a modifier of the matching operation.
The special characters for a character class are - ] / ^ $ (and the pattern delimiter, whatever it is). ] is special because it denotes the end of a character class. $ is special because it denotes a scalar variable. / is special because it is used in escape sequences.

/[/]c]def/; # matches ']def' or 'cdef'
$x = 'bcr';
/[$x]at/; # matches 'bat', 'cat', or 'rat'
/[/$x]at/; # matches '$at' or 'xat'
/[//$x]at/; # matches '/at', 'bat, 'cat', or 'rat'

The specia character '-' acts as a range operator within a character class.

/item[0-9]/; # matches 'item0' or ... or 'item9'
/[0-9bx-z]aa/; # matches '0aa', ..., '9aa',
# 'baa', 'xaa', 'yaa', or 'zaa'
/[0-9a-fA-F]/; # matches a hexadecimal digit
/[0-9a-zA-Z_]/; # matches a "word" character,
# like those in a Perl variable name

If '-' is the first or last character in a character class, it is treated as an ordinary character; [-ab], [ab-] and [a/-b] are all equivalent.
The special character ^ in the first position of a character class denotes a negated character class, which matches any characters but those in the brackets.

/[^a]at/; # doesn't match 'aat' or 'at', but matches
# all other 'bat', 'cat, '0at', '%at', etc.
/[^0-9]/; # matches a non-numeric character
/[a^]at/; # matches 'aat' or '^at'; here '^' is ordinary

/d matches a digit, not just [0-9] but also digits from non-roman scripts
/s matches a whitespace character, the set [/ /t/r/n/f] and others
/w matches a word character (alphanumeric or _), not just [0-9a-zA-Z_] but also digits and
characters from non-roman scripts
/D is a negated /d; it represents any other character than a digit, or [^/d]
/S is a negated /s; it represents any non-whitespace character [^/s]
/W is a negated /w; it represents any non-word character [^/w]
The period '.' matches any character but "/n" (unless the modifier //s is in effect, as explained
below).

//d/d:/d/d:/d/d/; # matches a hh:mm:ss time format
/[/d/s]/; # matches any digit or whitespace character
//w/W/w/; # matches a word char, followed by a
# non-word char, followed by a word char
/..rt/; # matches any two chars, followed by 'rt'
/end/./; # matches 'end.'
/end[.]/; # same thing, matches 'end.'

The alternation metacharacter | .To match dog or cat, we form the
regexp dog|cat. As before, Perl will try to match the regexp at the earliest possible point in the
string. At each character position, Perl will first try to match the first alternative, dog. If dog doesn't
match, Perl will then try the next alternative, cat. If cat doesn't match either, then the match fails and
Perl moves to the next position in the string.

"cats and dogs" =~ /cat|dog|bird/; # matches "cat"
"cats and dogs" =~ /dog|cat|bird/; # matches "cat"

() is grouping metacharacter.

/(a|b)b/; # matches 'ab' or 'bb'
/(ac|b)b/; # matches 'acb' or 'bb'
/(^a|b)c/; # matches 'ac' at start of string or 'bc' anywhere
/(a|[bc])d/; # matches 'ad', 'bd', or 'cd'
/house(cat|)/; # matches either 'housecat' or 'house'
/house(cat(s|)|)/; # matches either 'housecats' or 'housecat' or
# 'house'. Note groups can be nested.
/(19|20|)/d/d/; # match years 19xx, 20xx, or the Y2K problem, xx
"20" =~ /(19|20|)/d/d/; # matches the null alternative '()dd',
# because '20dd' can't match

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

perl程序设计技巧

perl程序设计技巧:1. Why does POE pass parameters as array slices http://poe.perl.org/POE_FAQ/calling_convention 2. perl regular expression fast referecnces metacharacters are {}[]() ^ $ . | * + a metacharacter can be matched by putting a backslash befo
推荐度:
标签: 技巧 设计 程序
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top