Professional Documents
Culture Documents
Perl
1
2
3
4
5
6
7
8
931 1
9-1
m/<regexp>/// = ~ !~
/<regexp>/ m
s/<regexp>/<substituteText>/ <regexp>
<substituteText>/<regexp>/<substituteText> s
tr/<charClass>/<substituteClass>/
/<charClass>
<substituteClass>
<tr>
tr/[0-9]/9876543210. 1223456789987654321
~(:does)
!~(:doesn't)
$scalarName
# substitute the character a for b, and return true if this can happern
$scalarName =~ s/a/b;
$scalarName =~ m/a;
# does the scalar $scalarName have an a in it?
$scalarName =~ tr/Atr/A-Z/aZ/a-z/; # translate all capital letter with lower case ones, and return
return ture
if this happens
$scalarName !~ s/a/b/;
# substitute the character a for b, and return false if this indeed
happens.
$scalarName !~ m/a/;
# does the scalar $scalarName match the character a? Return false
if it does.
$scalarName !~ tr/0# translate the digits for the letters a thru j, and return false
tr/0-9/a9/a-j/;
if this happens.
@elements b1b2b3b4b5
while(<$FD>) {print if (m/ERBOR/);}
error
if (grep(/pattern/, @lines)) {print the variable \@lines has pattern in it!\
it!\n;}
pattern
932 2
grep(m/variable/, @arrayName);
@arrayName ()
933 3
Perl
silly people P
silly P silly
moods t( thing ) things t
silly moods
9-2
$line = m/expression.*matching.*could.*be.*very.*expensive.*/
.* ()
934 4
$a = " ${variable}aha" ;
$a TESTaha$a
TESTaha
$expression = ' hello';
@arrayName = (' elem1', ' elem2');
$variable =~ m/$expression/; # this equals m/hello/;
$variable =~ m/@arrayName/;
m/@arrayName/; # this equals m/elem1 elem2/;
m/elem1 elem2/$" |
melem | elem2 elem elem2
$variable =~ m/\
m/\x01\
x01\27/;
# match binary character x01, and
# octal character 27.
$variable
# substitute three tabs for three spaces.
$variable =~ s/\
s/\t\t\t//;
Perl m//
(/)(())
$variable
$variable~ m//usr/local/bin/;
Perl /
$path =~ m/\
m/\/usr\
/usr\/local\
/local\/bin/;
$path /usr/local/bin
()
Perl m// s///
/(/)
(")
$variable =~ m"/usr/local/bin";
m"\
\"help\
$variable =~ m"
"help\"";
"
Perl {}()[]
$variable
$variable
$variable
=~
=~
=~
( emacs vi
)
quotemeta()
$variable =~ m" $scalar";
$scalar
$variable =~ m"({"
$scalar \(\{$scalar
$variable =~ m" \(\{" ;
({
935 5
()
Perl ($1,$2,$3$65536)
HIS Perl $1 $1
)
9-3
9-3
$example $backreferences
$1 $2 $example $backrefercences 94
$example $bacbreference
$example backreferences
if $1 $2
if ($text =~ m" (example).*(back)")
{
# prints ' example' -- since the first parens match the text example.
print $1;
print $2;
# prints ' back' -- since the second parens match the text back
}
$text
$text
print
$1 Perl $1
1) back
This is an example of backreferences'
exemplar
2)
Perl gotcha$1
( Perl )
1
2
3
4
5
6
7
# sets $1 to be bedbug.
$1 bedbug 5 nasti
Perl
3
(
)
1)' &&'
($scalarName =~ m" (nasti)" ) {$matched = $1;}
2)if if if
if ($scalarName =~ m" (nasti)" ) {$matched = $1;}
4
s" " " m" "Perl
(amp..) ample
ample example\1 sample examples
1) b \1 $1
2)\1 b b b
3)(a..) all \2 $2
4)\1 b
5)\2 all (all)
bballball$1 '
b'$2 all
$string = ' bballball' ;
$string =~ s" (b)\
(b)\1(a..)\
1(a..)\1\2" $1$2";
()
$string =~ s" (b)b(all)ball" ball";
bballball ball
9-5
5
()
m" ((aaa)*)" ;
* aaa",aaa,aaaaaa,aaaaaaaaaPerl
3a aa
$string = ' softly slowly surely subtly';
1)
$var =~ m" (a)(b)";
(a)$1(b)$2
2)
$var =~ m" (c(a(b)*)*)";
(m "(c(a(b)*)*)" )$1 a
m"(c(a(b)*)*)"$2(m"(c(a(b)*)*)" ) b $3
3) 1 $var =~ m" (a)(b(c))" (a)
$1b(c)$2(c)$3
(s...ly\s*)*$1(s...ly\s*)*$2
Perl
Perl
936 6
Perl
C
Perl (
)Perl
1
Kumquat
Kristina
Kentucky
Key
Keeping
Perl
[Kk]
Perl [
]"
"K""R"
a newline in it so it matches";
either a tab or a newline.
since the tab is present.
""
([t\n])
"\t"
"\n"
[]^
[ ]-([0-9]
([A-Z])
$a =~ m" [a
[a-fhfh-z]";
$a =~ m" [^0[^0-9a9a-zAzA-Z]";
$a =~ m" [0[0-9^A9^A-ZaZa-z]";
$a =~ m" [\
[\t\n]";
#
#
#
#
#
#
[09^AZaz]
[][][]
(1)
[0-9]Perl
\d ([0-9])
\D ([^0-9])
\w ([a-zA-Z0-9_])()
\W ([^a-zA-Z0-9_])
\s ([\t\n])()
\S ([\t\n])
- ()([^\n]) m"(.*)"S
$- ()
^-
\b,\B- $^(\b)(\B)
(.)
$a = ' NOW is the time for all good men to come to the aid of their party';
$a =~ m" (Now).*(party)";
# matches, since ' .' matches any
character except newline
and ' *' means match zero or
or more characters.
* Now party (
(greediness)
)
=~(
)
1
2
3
4
'
'
'
'
5 '
6 '
7 '
8 '
1956.23' =~ m"
333e+12' =~ m"
$hash($value)'
$hash($value)'
(\
(\d+)\
d+)\.(\
.(\d+)";
(\
(\D+)";
=~ m" \$(\
$(\w+){\
w+){\$(\
$(\w+)}";
=~ m" \$(\
$(\w+){(\
w+){(\w)*(\
w)*(\w+)(\
w+)(\w*)}";
# $1 = 1956, $2 = 23
# $1 = ' e+'
# $1 = ' hash', $2 = 'value'
hash',
h',
# $1 = ' $', $2 = ' has
# $3 = ' $', $4 = ' value'
VARIABLE = VALUE' =~ m" (\
(\w+)(\
w+)(\s*) = (\
(\s*)(\
s*)(\w+)"; # $1 = ' VARIABLE', #$2 = ' ',
# $3 = ' ', $4 = ' VALUE'
catch as catch can' =~ m" ^(.*)can$;
# $1 = ' catch as catch'
can as catch catch' =~ m"^can(.*)$
# $1 = ' as catch catch'
word_with_underlines word2' =~ m" \b(\
b(\w+)\
w+)\b;
# $1 = word_with_underlines
+
5 \s* 8
4
1 Perl Perl
(2)
9-2
99-2
\D
\d
\w
\W
\s
\S
' .'
9-3
^cat cat
$ziggurautString
$ziggurautString
$ziggurautString
$ziggurautString
() ziggurat
\W
\b\B
$example =
$example =~
' 111'.
$example =
' 111119';
m" \d\d\d";
# match the first three digits it can find in the string Matches
$example =~ m" of (\
(\w\w\w\w\w)";
of( words )
of of( numbers )
\w
Perl
2
PerI
\w
PerI
*
?
?
{X}X
{X}X
{XY}XY
$example
$example = ' This is a set of words and not of numbers';
$example =~ m" of (\
# Matches ' of word'.
(\w\w\w\w\w)";
$example =~ m" of (\
# Usage of {X} form. Matches 5 characters,
(\w{5})";
# and backreference
backreference $1 becomes the string ' words'.
a*
aaa
aaa a a
$example = ' this matches a set of words and not of numbers';
$example =~ m" of(\
of(\w+)";
words(of(\w+) eq of words)
$example
$example =~ m" of (\
(\w(2,3))";
m" "
$example = ' this matches a set of words and not of numbers';
$example =~ m" of(\
of(\d*)";
\d*\d*
\d+\d*
of
3
?Perl
Perl
$example = ' This is the best example of the greedy pattern match in Perl5';
is
$example =~ m
print $1;
$1 is
' is the best example of'
9-6
**
the( greedy )
$example
$example
$example
$example
$example
$example
RECORD
PerI ' RE' =~ m" \w{2,3}"
$string =~ m " has(.*)multiple(.*)wildcards";
()
1) has(m" has.*multiple.*wildcards")
2) multiple(m" has(.*)mMltiple(.*)
wildcards)
3) multiple(m" has(.*)multiple(.*)wildcards")
4) wildcards(m" has(.*)multiple(.*)wildcards")
5) wildcards(m" has(.*)multiple(.*)wildcards")
4)Perl wildcards
WILDCARDS does not match ' wildcards' !
has
has many multiple wildcards multiple WILDCARDS
^goes back here
6) multiple m"
has(.*)multiple(.*)wildcards"
has many multiple wildcards multiple WILDCARDS
8)m" has(.*)multiple(.*)wildcards")
has many multiple wildcards multiple WILDCARDS
9)wildcards(m" has(.*)multiple(.*)wildcards")
has many multiple wildcards multiple WILDCARDS
Perl ()
Perl
m" (.*)(.*)";
/* */
m" /\
/\*.*\
*.*\*/";
/* this is a comment*/
comment*/ /* another comment */
greedy
$commentmatcher =~ m" /\
/\*([^*]*|\
*([^*]*|\**[^/*])*\
**[^/*])*\*/";
( m" ")
?
commentmatcher
$commentmatcher =~ m" /\
/\*(.*?)\
*(.*?)\*/";
/**/
9-7
?
*/
(.*?)?(
*())(.*?)
(.*?)
(.\d[123])
*?
+?
??
{X}?X
{X, }?X
{X,Y}?XY
$example = This is the time for a11good man to come to the aid of their party ;
$example = ~ m This (.*?) the;
l ?
11999
$example = 1f999133333333333333331
1f999133333333333333331
if ($example = ~ m l (\
(\d{3,}?) )
{
}
1
f 1 1333
$example = 1f999133333333333333331;
if ($example = ~ m l (\
(\d{3,}?)1 )
{
}
\d(3,)
1
Perl
Perl
937 7
Perl
Per1
(able | baker | charlie)
$line = ~ m (. * | word );
word word .
.*
.
.*
$line = wordstar;
$line = ~ m (.*
(.* | word);
wordstar word.
.*.
.*
word wordstar word
$line = ~ m (word | .*); #since word is first.
$line
$line
$line
=
=
=
~m
words;
word;
word (s|$) sg; # word may be followed by the character ! or $.
word word s
$
$line = ~ m word (s | \b);
938 8
Perl ?..
Perl Perl 4 Perl5
Perl
<special character(s)><text>
9-5
(?#comment) x
()
(?! Perl
$line = ~ m (?! Oranges ) I love ;
(?:
:)(?=
) (?:
:)
:
:
(?:
:)
(\w)1 $2 (?)
(?:
:) g g
<data><data2>()<data3>
$line = ~ m BLOCK \d(,*?) (BLOCK\
(BLOCK\d | $) g;
<data> BLOCK
BLOCKl BLOCK2
BLOCK2<data2>
9-8 (?)
(?=)
BLOCK 1 <data>
<data> BLOCK2 <data2> BLOCK3 <data3>
while ($line = ~ m BLOCK\
BLOCK\d(.*?) (?=BLOCK\
(?=BLOCK\d | $ )g)
{
print $1 \n;
}
<data><data2><data3> g
1((m//)(s///)(tr///))
2($scalar=~ m" a"; ; @array=~ m" a"
@array )
3
($a = ' string string2'; $a=~s" string" "; $a==' 1 string
2')
4($a =~ m" $varb"
varb $varb=' a' $a=' as' $a=~S" $varb" "
$a s)
5$a=~
m"varb" $a varb $a=~s"(word2)(word1)" $1 $2"
6$a =~
m" \w+" $a=~m" \d*"
7Perl (alternation)
m" (cat|dog)" cat " dog"
8Perl (?..)
$a =~ m" ERROR"
$a ERRORC
.
9310
$a
$a~m//
~m//# m"
"is synony2n
synony2nas is m { }
$a
$a~S///
~S/// # s" " " is synonym
synonymas is s { } { }
a b
$a =~ m//gismxo;
m//gismxo; $a =~ s///geimxo;
Perl
(s,m,i,x,o)
(e,g)
1
m" " s" " " 9-6
(1)x
--- Perl
$line
$line~m" sub\
sub\s+(w+)\
s+(w+)\s+{(.*?)}\
s+{(.*?)}\s*(?=sub)" s;
? Perl
Perl4 x x
x :
$line =~ m" sub\
sub\s+(\
s+(\w+)\
w+)\s+{(.*?)}\
s+{(.*?)}\s*(?=sub)" s;
$line =~ m{
sub\
sub\s+ ( \w+ ) \s+
{
( .*?)
}
\s*(?=sub)
}sx;
.,
x ( s{}{})
$line = 'aaaaaa' ;
$line = ~ s{
a
}
{
b
}
gx;
a (
) 3 b
(2)i *
i
$pattern
$pattern
$pattern
$pattern
= ' Exercise' ;
=~ s"exer" EXER" i'
= ' Edward Peschko' ;
== ???
# matches first
first four characters of Exercise.(Exer)
$line =
BLOCK1:
<text here1>
END BLOCK
BLOCK2:
<text here2>
END BLOCK'
text here[0-9
$line =~m{
BLOCK(\
BLOCK(\d+)
(.*?)
END\
END\ BLOCK
(.)
,(.)
(\s)[\n\t]
s Perl
s .
$line =~ m{
BLOCK(\
BLOCK(\d+)
(.*?)
END\
END\ BLOCK
}s;
s
(4)m
m s ,:"
^$()^
$
$line = ' a
b
c';
$line = ~m"
????
m $ a a\nb\nc
(5)o
o
$line =~ m" <very long expression>" ;
Perl $regex (,
regexp o
).
2
(smxi o)(s///,m//)
g e
(1)g
s///
g
$pattern
$pattern'NUM1 NUM2 NUM3
NUM3
$pattern
~s" NUM" LETTER" g
$pattern
g #substitutes NUM for LETTER
LETTER
$pattern
$pattern~s" num" LETTER" gi
gi #Note -- you can stack these modifiers
modifiers
#does exactly the same thing as the above
above
while($pattern
while($pattern~s" NUM" LETTER"){}
ASCII
$string
$string'hello';
$string
~ s{ ( \w)}
#we save the $1
$string
$1
{ord($1):={ord($1):=-" ";}egx;
?
$string
$stringturnToAscii($string);
sub turnToAscii
{
my($string)
my($string)@_;
my($return
my($return@letters);
@letters);
@ letters = split(//,$string);
foreach $letter (@letters)
{
$letter = ord($letter)." "if ($letter = ~ m" \w" );
}
$return = join(' ',@letter);
$return;
}
e
9311 g
(xis e) m//
g
,Perl m//
s/// g
Perl g $string~m" " g Perl
Perl
,
$line = " hello stranger hello friend hello sam";
while($line=~ m" hello (\
(\w+)" sg)
{
print " $l\
$l\n"
n"
}
stranger
friend
sam
g
$line = " hello";
while($line=~ m" hello" sg)
{
$line = $line;
}
!()
93.12
Perl
Perl
{print
{print" Substituted Correctly" ;}
Substituted Correctly if l
a b
2(g )
undef $/;
$/;
my $fh = new FileHandle(" File1");
my $fh2 = new FileHandle(" File2");
(($line = <$fh>) =~ s" a" b") == (($line2 = <$fh2>) =~ s" a" b");
a
3()(g )
4()
$line a
if($line
if($line~m" \b(\
b(\w+)\
w+)\b"){print " $l\
$l\n";}
$line $1
($line
($line~m" \b(\
b(\w+)\
w+)\b")&&(print " $l\
$l\n";);
5.()
(\w+)$variable()
$equals;(\w+)$value
6(g )
$fd
7(g )
Perl
$line = " BEGIN <data> BEGIN <data2> BEGIN <data3>";
while($line =~ m" BEGIN(.*?)(?=BEGIN|$)"
BEGIN(.*?)(?=BEGIN|$)" sg)
{
push(@blocks, $l);
}
() while @blocks
BEGIN <data> (%)BEGIN <data2> BEGIN <data3>
BEGIN <data> BEGIN <data2>(%) BEGIN <data3>
BEGIN <data> BEGIN <data2>(%) BEGIN <data3>
(%)(?=)