10
Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression / pattern matching 3. To learn the special cases occurred in regular expression / pattern matching

Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

Embed Size (px)

Citation preview

Page 1: Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

Regular Expression (2)

Learning Objectives:1. To understand the concept of regular

expression

2. To learn commonly used operations involving regular expression / pattern matching

3. To learn the special cases occurred in regular expression / pattern matching

Page 2: Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

COMP111COMP111Lecture 16 / Slide Lecture 16 / Slide 22

Another simple regular expression is the substitute operator. It replaces part of a string that matches the regular expression

with another string.

s/Shakespeare/Bill Gates/; $_ is matched against the regular expression (Shakespeare). If the match is successful, the part of the string that matched is

discarded and replaced by the replacement string (Bill Gates).

If the match is unsuccessful, nothing happens.

Substitution (1)

Page 3: Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

COMP111COMP111Lecture 16 / Slide Lecture 16 / Slide 33

The program:$ cat movieTitanicSaving Private RyanShakespeare in LoveLife is Beautiful$ cat sub1#!/usr/local/bin/perl5 -wwhile(<>){

if(/Shakespeare/){s/Shakespeare/Bill Gates/;print;

}}$ sub1 movieBill Gates in Love$

Substitution (2)

Page 4: Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

COMP111COMP111Lecture 16 / Slide Lecture 16 / Slide 44

An even shorter way to write it:

$ cat sub2#!/usr/local/bin/perl5 -wwhile(<>){

if(s/Shakespeare/Bill Gates/){print;

}}$ sub2 movieBill Gates in Love$

Substitution (3)

Page 5: Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

COMP111COMP111Lecture 16 / Slide Lecture 16 / Slide 55

If you want to replace all matches instead of just the first match, use the g option for substitution:

$ cat sub3#!/usr/local/bin/perl5 -w$_ = "Bill Shakespeare in love with Bill Gates";s/Bill/William/; print "Sub1: $_\n";$_ = "Bill Shakespeare in love with Bill Gates";s/Bill/William/g; print "Sub2: $_\n";$ sub3Sub1: William Shakespeare in love with Bill GatesSub2: William Shakespeare in love with William Gates$

All Matches in Substitution (1)

Page 6: Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

COMP111COMP111Lecture 16 / Slide Lecture 16 / Slide 66

You can use variables in substitutions:

$ cat sub4#!/usr/local/bin/perl5 -w$find = "Bill";$replace = "William";$_ = "Bill Shakespeare in love with Bill Gates";s/$find/$replace/g; print "$_\n";$ sub4William Shakespeare in love with William Gates$

Variables in Substitution (2)

Page 7: Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

COMP111COMP111Lecture 16 / Slide Lecture 16 / Slide 77

Pattern characters in the regular expression allows patterns to be matched, not just fixed characters:

$ cat sub5#!/usr/local/bin/perl5 -w$_ = "Bill Shakespeare in love with Bill Gates";s/(\w+)/<$1>/g; print "$_\n";$ sub5<Bill> <Shakespeare> <in> <love> <with> <Bill> <Gates>$

Memory $1 in Substitution (3)

Page 8: Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

COMP111COMP111Lecture 16 / Slide Lecture 16 / Slide 88

Other characters in Substitution (4) Substitution also allows you to:

ignore case use alternate delimiters use =~

$ cat sub6#!/usr/local/bin/perl5 -w$line = "Bill Shakespeare in love with bill Gates";$line =~ s#bill#William#gi; $line =~ s@Shakespeare@Gates@gi; print "$line\n";$ sub6William Gates in love with William Gates$

Page 9: Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

COMP111COMP111Lecture 16 / Slide Lecture 16 / Slide 99

foreach $line (@weblog) {if ($line =~ /(^http:\/\/\w*)/) {

print “URL: $1\n”;

}

}

How to say it in English?

Page 10: Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression

COMP111COMP111Lecture 16 / Slide Lecture 16 / Slide 1010

Replacing uppercase to lower case

#!/usr/local/bin/perl5 -w

while ($line=<>) { $line =~ s/([A-Z])/\L$1/g; print $line, "\n";}

\L$1 means to replace all uppercase in $1 to lowercase characters