Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Regular Expression (2) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression."— Presentation transcript:

1 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

2 COMP111 Lecture 16 / Slide 2  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)

3 COMP111 Lecture 16 / Slide 3  The program: $ cat movie Titanic Saving Private Ryan Shakespeare in Love Life is Beautiful $ cat sub1 #!/usr/local/bin/perl5 -w while(<>){ if(/Shakespeare/){ s/Shakespeare/Bill Gates/; print; } $ sub1 movie Bill Gates in Love $ Substitution (2)

4 COMP111 Lecture 16 / Slide 4  An even shorter way to write it: $ cat sub2 #!/usr/local/bin/perl5 -w while(<>){ if(s/Shakespeare/Bill Gates/){ print; } $ sub2 movie Bill Gates in Love $ Substitution (3)

5 COMP111 Lecture 16 / Slide 5  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"; $ sub3 Sub1: William Shakespeare in love with Bill Gates Sub2: William Shakespeare in love with William Gates $ All Matches in Substitution (1)

6 COMP111 Lecture 16 / Slide 6  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"; $ sub4 William Shakespeare in love with William Gates $ Variables in Substitution (2)

7 COMP111 Lecture 16 / Slide 7  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+)/ /g; print "$_\n"; $ sub5 $ Memory $1 in Substitution (3)

8 COMP111 Lecture 16 / Slide 8 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"; $ sub6 William Gates in love with William Gates $

9 COMP111 Lecture 16 / Slide 9 foreach $line (@weblog) { if ($line =~ /(^http:\/\/\w*)/) { print “URL: $1\n”; } How to say it in English?

10 COMP111 Lecture 16 / Slide 10 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


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

Similar presentations


Ads by Google