Pages

Friday, September 14, 2012

PERL Regular Expression

Modifiers
m      Matching
m/.../

s        Substitution
s/.../.../

i        Do case-insensitive pattern matching.
.../i

g       Global matching.
.../g

Meta-characters

           \        Quote the next metacharacter
           ^       Match the beginning of the line
           .        Match any character (except newline)
           $       Match the end of the line (or before newline at the end)
           |        Alternation
           ()      Grouping
           []      Bracketed Character class

Logical
           [^a-z]     No lowercase letters
            abc|def  abc or def

Sequence
         \w       Match a "word" character (alphanumeric plus "_",
                    plus other connector punctuation chars plus Unicode marks)
         \W      Match a non-"word" character
         \s        Match a whitespace character
         \S       Match a non-whitespace character
         \d       Match a decimal digit character
         \D      Match a non-digit character

Quantifiers
           *           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


Examples
           s/^([^ ]*) *([^ ]*)/$2 $1/;     # swap first two words
           (ha)+     #Either ha or haha or hahaha or ...
           (eg|le)gs     #Either eggs or legs

No comments:

Post a Comment