Home » Perl, Regular Expression15 August 2007

Fun with Regular Expression

Fun with perl regular expression

A) $st = “The programming republic of perl”;

$1 = ?, $2 = ? and $3 = ?

1) if($st =~/(.*)(e|r)(.*)$/)

2) if($st =~/(m{1,2})(.*)$/)

3) if($st =~/(.*)(m{1,2})(.*)$/)

4) if($st =~/(.?)(m{1,2})(.*)$/)

5) if($st =~/(.+?)(e|r)(.*)$/)

6) if($st =~/(m{1,2}?)(.*?)$/)

7) if($st =~/(m{1,2}?)(.*?)/)

8 ) if($st =~/(.*})(.*)$/)

Answer:

1) $1 = The programming republic of pe $2 = r $3 = l

2) $1 = mm $2 = ing republic of perl $3 = –

3) $1 = The program $2 = m $3 = ing republic of perl

4) $1 = a $2 = mm $3 = ing republic of perl

5) $1 = Th $2 = e $3 = programming republic of perl

6) $1 = m $2 = ming republic of perl $3 = –

7) $1 = m $2 = – $3 = –

8 ) $1 = The programming republic of perl $2 = – $3 = –

B) $st = “Mississippi made a hissing sound”;

$1 = ?, $2 = ? and $3 = ?

1) if($st =~/(.*)(i|s)(.*)$/)

2) if($st =~/(s{1,2})(.*)$/)

3) if($st =~/(.*)(s{1,2})(.*)$/)

4) if($st =~/(.?)(s{1,2})(.*)$/)

5) if($st =~/(.+?)(i|s)(.*)$/)

6) if($st =~/(s{1,2}?)(.*?)$/)

7) if($st =~/(s{1,2}?)(.*?)/)

8 ) if($st =~/(.*})(.*)$/)

Answer:

1) $1 = Mississippi made a hissing $2 = s $3 = ound

2) $1 = ss $2 = issippi made a hissing sound $3 = –

3) $1 = Mississippi made a hissing $2 = s $3 = ound

4) $1 = i $2 = ss $3 = issippi made a hissing sound

5) $1 = m $2 = i $3 = ssissippi made a hissing sound

6) $1 = s $2 = sissippi made a hissing sound $3 = –

7) $1 = s $2 = – $3 = –

8 ) $1 = Mississippi made a hissing sound $2 = – $3 = –

C) $st = “Yahoo shooting hotmail semeeingly”;

$1 = ?, $2 = ? and $3 = ?

1) if($st =~/(.*)(h|o)(.*)$/)

2) if($st =~/(o{1,2})(.*)$/)

3) if($st =~/(.?)(e{1,2})(.*)$/)

4) if($st =~/(e{1,2}?)(.*?)$/)

Answer:

1) $1 = Yahoo shooting h $2 = o $3 = tmail semeeingly

2) $1 = oo $2 = shooting hotmail semeeingly $3 = –

3) $1 = s $2 = e $3 = meeingly

4) $1 = e $2 = meeingly $3 = –

D) $st = “zzen zest zzep zzengz”;

$1 = ?, $2 = ? and $3 = ?

1) if($st =~/(.*)(z|e)(.*)$/)

2) if($st =~/(z{1,2})(.?)(.*)$/)

3) if($st =~/(.*)(z{1,2})(.*)$/)

4) if($st =~/(.+?)(z|e)(.*)$/)

5) if($st =~/(z{1,2}?)(.*?)$/)

Answer:

1) $1 = zzen zest zzep zzeng $2 = z $3 = –

2) $1 = zz $2 = e $3 = n zest zzep zzengz

3) $1 = zzen zest zzep zzeng $2 = z $3 = –

4) $1 = z $2 = z $3 = en zest zzep zzengz

5) $1 = z $2 = – $3 = –

E) $test = “Communication comparison ammplification,”

$1 = ?, $2 = ? and $3 = ?

1) if($test =~/(.*)(m{1,2})(.*)/)

2) if($test =~/(.*?)(.*)/)

3) if($test =~/(.?)(m|p)(.*)/)

4) if($test =~/(.?)(^m|p)(.*?)/)

Answer:

1) $1 = Communication comparison am $2 = m $3 = plification,

2) $1 = $2 = Communication comparison amplification, $3 =

3) $1 = o $2 = m $3 = munication comparison amplification,

4) $1 = m $2 = p $3 =

F) $ip = “progra0mming456!is-+?fun!”

a) $ip =~tr/A-z/*/s;

b) $ip =~tr/A-z/ /cs;

c) $ip =~tr/A-z//sc;

Find the value of a),b) and c).

Answer:

a) $ip = *0*456!*-?*!

b) $ip = progra mming is fun

c) $ip = progra0mming456!is-+?fun!

Regular Expression

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed