Perl programming skills

Source: Internet
Author: User
Tags character classes perl regular expression
1. WhydoesPOEpassparametersasarrayslices? Poe.perl.org? POE_FAQcalling_convention2.perlregularexpressionfastreferecncesmetacharactersare {} [] () ^ $. | * +? Ametacharactercanbematchedbyputtingabackslashbefo

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
MetacharactersAre

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

A metacharacter can be matched by putting a backslash before it
AnchorMetacharacters ^ 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 particle 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'
/[CAA] at/; # matches 'batt', '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 = 'abc ';
/[$ X] at/; # matches 'batt', '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 '0a',..., '9a ',
# '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/; # matches a hh: mm: ss time format
/[/D/s]/; # matches any digit or whitespace character
// W/W/w/; # matches a word char, followed by
# 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 AlternationMetacharacter |. To match dog or cat, we form
Regexp dog | cat. As before, Perl will try to match the regexp at the earliest possible point in
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 GroupingMetacharacter.

/(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/; # match years 19xx, 20xx, or the Y2K problem, xx
"20" = ~ /(19 | 20 |)/d/; # matches the null alternative '() dd ',
# Because '20dd' can't match

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.