You are on page 1of 28

Perl

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.

horned toad =~ m/toad/ 9-1


$_ ( while map grep
)!~=~
my @elemente = (' al' ,' a2' ,'
,' a3' ,' a4' ,' a5' );
foreach (@elements) {s/a/b/;}

@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

@arrayName = (' variablel', 'variable2');


@arrayName =~ m/variable/; # looks for ' variable' in the array? No! use grep instead

@arrayName ! @arrayName Perl 2


' 2' =~ m/variable/;

grep(m/variable/, @arrayName);

@arrayName ()

933 3

Perl

' Silly people do silly things if in silly moods'

' silly moods'

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

s///(s/*//) m// m/*/


(!
)
$variable = ' TEST' ;
$a =~ m/${variable}aha/;

$a = " ${variable}aha" ;

$a TESTaha$a
TESTaha
$expression = ' hello';
@arrayName = (' elem1', ' elem2');
$variable =~ m/$expression/; # this equals m/hello/;

$expression hello 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/;

# matches /usr/local/bin? NO! SYNTAX ERROR

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\"";

# Note the quotation marks.


# If you are going to match quotation
quotation
# marks, you need to backslash them here. (as per\
per\")
$variable =~ S" $variable" $variable";
# works in s///too.

"

Perl {}()[]
$variable
$variable
$variable

=~
=~
=~

m{this works well with vi or emacs because the parens bounce};


m(this also works well);
s(substitute pattern) {for this pattern}sg;

( emacs vi
)
quotemeta()
$variable =~ m" $scalar";

$scalar

$scalar = " ({";

$variabie =~ m" $scalar" ;

$variable =~ m"({"

$scalar = quotemeta(' ({');

$scalar \(\{$scalar
$variable =~ m" \(\{" ;

({
935 5


()

$pattern = ' simple always simple';


$result = ($pattern =~ m"simple");

result 1 simple simple always simPle simple


always simple
$result = ($pattern =~ m" complex");

result complex simple always simple


$result = ($pattern =~ s" simple" complex");

result 1 simple complex


$pattern = ' simple simple';
$result = ($pattern =~ s" simple" complex" g);

$result 2 simple always simple simple


g
(
)
$pattern = ' simple still';
if ($pattern
($pattern =~ m" simple")
{
print " MATCHED!\
MATCHED!\n";
}

if $pattern =~ m" simple"


Perl$pattern
simple Matched!
2
Perl
(())

Perl ($1,$2,$3$65536)

$text = " this matches ' THIS' not 'THAT' ";


$text =~ m" ('TH..' )";
print " $1\
$1\n";

HIS Perl $1 $1

1)((.)) THIS (TH..)


THAT
2)THIS
regexp THIS (

)
9-3
9-3

$text = ' This is an example of backreferences';


($example, $backreferences) = ($text =~ m" (example).*(backreferences)" );

$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

= ' This is an example of backreferences';


=~ s" (examplar).*(back)" doesn't work";
$1;
$1;

$1 Perl $1

1) back

This is an example of backreferences'

exemplar
2)
Perl gotcha$1
( Perl )

1
2
3
4
5
6
7

$a = ' bedbugs bite';


$a =~ m" (bedbug)";
$b = ' this is nasty';
$b =~ m" (nasti)";
print $1;

# sets $1 to be bedbug.

# does NOT set $1 (nasti is not in ' this is nasty' ).


# BUT $1 is still set to bedbug!
# prints ' bedbug'.

$1 bedbug 5 nasti
Perl
3
(

)
1)' &&'
($scalarName =~ m" (nasti)" ) {$matched = $1;}

2)if if if
if ($scalarName =~ m" (nasti)" ) {$matched = $1;}

else {print "$scalarName didn't match"; }


3)
($match1, $match2) = ($scalarName =~ m" (regexp1).*(regexp2)" );

4
s" " " m" "Perl

s" " "()$1$2 m" "


s" " "()\1\2
$string = ' far out';
$string =~ s "(far)(out)" $2 $1";

# This makes string ' out far'.

far out out far


$string = ' sample examples';
(amp..)
) ex\
if ($string =~ m" (amp..
ex\1") {print " MATCHES!\
MATCHES!\n"; }

(amp..) ample
ample example\1 sample examples

$string = ' bballball';


$string =~ s" (b)\
(b)\1(a...)\
1(a...)\1\2" $1$2";

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

s" " " Perl

5
()

m" ((aaa)*)" ;

* aaa",aaa,aaaaaa,aaaaaaaaaPerl
3a aa
$string = ' softly slowly surely subtly';

$string = m" ((s...ly\


((s...ly\s*)*)" ;

# note nested parens.

softly slowly surely subtly


s ly ly
surely 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

$string = 'softly slowly surely subtly'


$string = m"(((s...ly
m"(((s...ly\
\s*)*)";
# note nested parens.

(s...ly\s*)* softly slowly


surely subtly(s...ly\s*)* Perl
$2 subtly

Perl
Perl
936 6

Perl

C
Perl (
)Perl
1

Kumquat
Kristina
Kentucky
Key
Keeping

Perl
[Kk]

Perl [
]"
"K""R"

$scalarName = ' this has a digit (1) in it';


# This matches any character between 0 and 9, that is matches
$scalarName =~ m" [0[0-9]";
any digit.
$scalarName =~ 'this has
has a capital letter (A) in it';
$scalarName =~ m" [A# This matches any capital letter (A[A-Z]";
(A-Z).
$scalarName =~ " this does not match, since the letter after the string 'AN' is an A"
$scalarName =~ m" an[^A]";

[0-9] this has a digit(1) in it 1[AZ]


this has acapital letter(A) in it A
an an A
an[^A] a n
A match an A not an e
an
$scalarName = " This has a tab(
) or
$scalarName =~ m" [\
# Matches
[\t\n]"
# matches

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]";

#
#
#
#
#
#

matches any lowercaes letter * except*


except* g.
matches any nonword character. (i.e.,NOT
a character in 00-9, aa-z or AA-Z)
a mistake, Does not
equal the above. Instead
Instead matches 00-9,
matches a space character: tab, newline or blank).

[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

= ' this matches the word zigguraut';


=~ m" \bzigguraut\
bzigguraut\b";
=~ m" \Wzigguraut\
Wzigguraut\W";

() ziggurat

\W

$ziggurautString = " This matches the word zigguraut now";


$ziggurautString =~ s" \Wzigguraut\
Wzigguraut\W" " g;

This matches the wordnow

\b\B

$example =
$example =~
' 111'.
$example =

' 111119';
m" \d\d\d";

# match the first three digits it can find in the string Matches

' This is a set of words and not of numbers';

$example =~ m" of (\
(\w\w\w\w\w)";

# Matches ' of words' ..Creates a backreference

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))";

# Usage of {X,Y}. Matches the string ' wor'


# (the first three letters of the first match it finds.
matches the string ' wor' (' of \w{2,3}' equals ' of wor' here)

m" "
$example = ' this matches a set of words and not of numbers';
$example =~ m" of(\
of(\d*)";

\d*\d*

$example =~ m" of(\


of(\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;

# This (.*) the#;


# This does NOT print out the string ' is'!

$1 is
' is the best example of'

9-6

**
the( greedy )

$example
$example
$example
$example
$example
$example

= ' sam I am';


=~ m" (.*)am';
= ' RECORD: 1 VALUE: A VALUE2: B';
=~ m".RECORD';(.*)VALUE";
= ' RECORD';
=~ m" \w{2,3}";

# matches the string ' sam I'


# matches ' 1 VALUE: A';
# matches
matches REC

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")

has many multiple wildcards multiple WILDCARDS

1)Perl has(i.e, m" has(.*)multiple(.*)wildcards);


has many
many multiple wildcards multiple WILDCARDS

2)Perl m" has(.*)multiple(.*)wildcards


multiple
has many multipIe wildcards multiple WILDCARDS

3)Perl multiple( m" has(.*)multiple(.*)wildcards):


has many multiple wildcards
wildcards multiple WILDCARDS

4)Perl wildcards
WILDCARDS does not match ' wildcards' !

5) Perl (*) Perl


2
has many multiple wildcards multiple WILDCARDS

has
has many multiple wildcards multiple WILDCARDS
^goes back here

6) multiple m"
has(.*)multiple(.*)wildcards"
has many multiple wildcards multiple WILDCARDS

7) m" has(.*)multiple(.*)wildcards" multiple


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

has many multiple wildcards


Perl

Perl ()
Perl

$pattern = " afbgchdjafbgche";


afbgchdjafbgche";
$pattern =~ m" a(.*)b(.*)c(.*)d";

afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";)


afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";) -- greedy, goes to last ' b'
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";)
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";)
a(.*)b(.*)c(.*)d";) -- matches g
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";)
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";) -- backtrack because no ' d'
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";) -- now we take up everything to the next to last b
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";)
a(.*)b(.*)c(.*)d";)
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";) -- now the second .* becomes greedy.
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";)
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";) -- still no d.darn.backtracks
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";)
a(.*)b(.*)c(.*)d";) -- wildcard becomes less greedy, gobbles to next to
last c
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";)
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";) -- there is only one d in the expression and this
matches up to it
afbgchdjafbgche (m" a(.*)b(.*)c(.*)d";)
a(.*)b(.*)c(.*)d";) -- a match!
matches ' afbgchd'.

m" (.*)(.*)";

-Dr Perl - Dr.script.p


21
perl()
5
C (
FAQ )
/* this is a comment*/ /* another comment */

/* */

m" /\
/\*.*\
*.*\*/";

/* this is a comment*/
comment*/ /* another comment */

greedy

$commentmatcher =~ m" /\
/\*([^*]*|\
*([^*]*|\**[^/*])*\
**[^/*])*\*/";

( m" ")

?
commentmatcher
$commentmatcher =~ m" /\
/\*(.*?)\
*(.*?)\*/";

/**/
9-7

?
*/

$line =~ m" (.*?)(.*?)(.*?)";

(.*?)?(
*())(.*?)
(.*?)
(.\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;

is is the time for all good man to come to


$example = 19992113333333333333331
19992113333333333333331
if ($example = ~ m l (\
(\d{3,}?) )
{
}

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)

Per1 able baker charlie

$declaration = char string [80] ; or $declaration = unsigned char string [80];

char unsigned char

foreach $declaration ( char string [80], unsigned char string [80] )


{
if ($declaration = ~ m (unsigned char | char ) )
{
print :$1: ; # prints :char : first time
time around.
# prints :unsigned char : second time around .
}
}

| unsigned char char

$declaration = unsigned char string [80];


$declaration = ~ m (char | unsigned char );

unsigned char unsigned char string [80]


char unsigned char string [80]
char
unsigned char char

$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>)

<special character(s)><text>
9-5

(?#comment) x
()

$line = ~ m I love(?! oranges) ;

(?! oranges I love love figs I love


oranges I love orange I love ripe oranges
oranges
$line = ~ m I love (?!. * orange );

(?! Perl
$line = ~ m (?! Oranges ) I love ;

oranges I love(?l) oranges



oranges

(?:
:)(?=
) (?:
:)
:
:

$line = ~ m (?:int | unsigned int | char )\


)\s*(\
s*(\w+);

(?:
:)
(\w)1 $2 (?)

(?:
:) g g

BLOCK1 <data> BLOCK2 <data2> BLOCK3 <data3>

<data><data2>()<data3>
$line = ~ m BLOCK \d(,*?) (BLOCK\
(BLOCK\d | $) g;

<data> BLOCK

$line = ~ m BLOCK \d (.*?) (?=BLOCK\


(?=BLOCK\d)g;

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

9-8 m" BLOCK(.*?)(?=BLOCK)" g m" BLOCK(.*?)BLOCK" g


939

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+
{

# matches the ' sub' keyword, subroutine name


# and matches the white space afterwords.
# opening brace

( .*?)
}
\s*(?=sub)
}sx;

# matches the text of the sub. and saves it


# further use.
# closing brace
# the next sub keyword

.,

$line =" multi line string\


string\nhere" ;
$line =~ m" multi line string" X;

# this does not match the above because


# the space above GETS MUNGED OUT.

x ( s{}{})

$line = 'aaaaaa' ;
$line = ~ s{
a
}
{
b
}
gx;

# we want 'bbbbbb' after the substitute


substitute below.

# we want to do a 'general' match, i.e., match


# ALL a's for b's. DOESN'T WORK!
# prints '
. b
print $line;
$line;
b...etc'
# six times over.

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)

Exercise XERcise Edward


Peschko Edmund Peschko
i *pattern~ m" [Ee]
[Xx][Ee][Rr]"
(3)s
(.)

$line =
BLOCK1:
<text here1>

END BLOCK
BLOCK2:
<text here2>
END BLOCK'

text here[0-9
$line =~m{
BLOCK(\
BLOCK(\d+)
(.*?)
END\
END\ BLOCK

# Note backslash, Space will be ignored otherwise

(.)
,(.)
(\s)[\n\t]
s Perl
s .
$line =~ m{
BLOCK(\
BLOCK(\d+)
(.*?)
END\
END\ BLOCK

# Note backslash, Space will be ignored otherwise

}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>" ;

<very long expression> Perl

Jeffreu Friedl e-mail


e-maiI 6598 ! o ,

$line = ~m" $regex" o;

Perl $regex Perl


$regex = 'b';
while
while ('bbbbb' =~ m" $regex o) { $regex = 'c';}

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"){}

$pattern LETTERl LETTER2 LETTER3 1


(gi )( s" NUM" LETTER" NUM1
LETTER2 LETTER3 NUMl NUM2 LETTER3 NUM1 NUM2 NUM3)
(2)e s/// Perl
s/// e

ASCII
$string
$string'hello';
$string
~ s{ ( \w)}
#we save the $1
$string
$1
{ord($1):={ord($1):=-" ";}egx;

104 101 108 108 111 ord

?
$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

if($string =~ s" a" b")

{print
{print" Substituted Correctly" ;}

Substituted Correctly if l
a b
2(g )

($string =~ s" a" b" g) == ($string =~ s" a" b" g)

Perl $string $string2 a

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()

if($line =~ m"a") {print "Matched an a!\


a!\n" ;}

$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.()

($variable,$equals,$value) = ($line = ~m" (\


(\w+)\
w+)\s*(=)\
s*(=)\s*(\
s*(\w+)");

(\w+)$variable()
$equals;(\w+)$value
6(g )

$line = '1.2 3.4 beta 5.66';


@matches = ($line =~m" (\
(\d*\
d*\.\d+)" g);

@matches 1.23.45.66g 1.2 3.4


5.6:
undef $/;
mymy-$FD= new FileHandle(" file");
@comments
\*(.*?)\
@comments = (<$FD> =~m" //\
*(.*?)\*/");

$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>

(%)(?=)

You might also like