Top Banner
Regular Expression (cont.) Perl Programming for Linux Open Systems and the WWW VSRivera IBM Learning Services Worldwide Certified Manual
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PERL Unit 6 regular expression

Regular Expression (cont.)

Perl Programming for Linux Open Systems and the WWW

VSRiveraIBM Learning ServicesWorldwide Certified Manual

Page 2: PERL Unit 6 regular expression

Match Operator

Returns true or false depending on whether its regular expression matches a string

The default match target is $_ Syntax

/regular_expression/ m#regular_expression # m{regular_expression}

Page 3: PERL Unit 6 regular expression

Match Operator

Example m/^test(.*) / /^test(.*)/ ; m#test(.*)# m{^ test(.*)} m(^ test(.*)) m>^ test(.*)>

Page 4: PERL Unit 6 regular expression

Match Operator Example

#!/usr/bin/perl -w print ("Enter a an expression:\n"); $_=<>;chomp($_); if(m{^regular(.*)} ) { print "$_ is a valid expression\n"; }else { print "$_ is not a valid expression\n"; }

Page 5: PERL Unit 6 regular expression

Pattern Binding Operator

Specifies the target of a match operation

Syntax $variable =~ /reg_expr/ $variable !~ /reg_expr/

Page 6: PERL Unit 6 regular expression

Pattern Binding Operator Example

#!/usr/bin/perl -w print ("Continue ?: "); $_=<>;chomp($_); if($_ =~ /^[Yy]/) { print "Your answer is Y\n"; }else { print "Your answer is not Y \n"; }

Page 7: PERL Unit 6 regular expression

Pattern Binding Operator

#!/usr/bin/perl -w print ("Continue ?: "); $_=<>;chomp($_); if($_ !~ /^[Yy]/) { print "Your answer is not Y\n"; }else { print "Your answer is Y \n"; }

Page 8: PERL Unit 6 regular expression

Pattern Binding Operator

#!/usr/bin/perl -w$ans='x';while ($ans !~/^[YyNn]/){ print "Please enter 'Y'es or 'N'o: ";chomp($ans=<>);}

Page 9: PERL Unit 6 regular expression

Pattern Binding Operator

#!/usr/bin/perl print "Enter 'Y'es : ";if (($ans = <>) =~ /^[Yy]/){ print "Yes" ;}else{ print "Not Yes"; }

Since $ans is only used once, -w was omitted so that perl will not report the error

Page 10: PERL Unit 6 regular expression

Pattern Binding Operator

#!/usr/bin/perl $mystring = "Hello world!";print "Is there a word 'World' in the string

'$mystring'? ";if($mystring =~ m/World/i) { print "Yes"; }else{ print "No";}

Page 11: PERL Unit 6 regular expression

Pattern Binding Operator

#!/usr/bin/perl $age = "I fear that I'll live only for 15 years.";print "$age\n";if ($age =~ /(\d*) years/) { print "I'll live only for '$1' years.\n"; }

I fear that I'll live only for 15 years.I'll live only for ‘15' years

Page 12: PERL Unit 6 regular expression

Pattern Binding Operator

#!/usr/bin/perl $age = "I fear that I'll live only for a few

years.";print "$age\n";if ($age =~ /(\d*) years/) { print "I'll live only for '$1' years.\n"; }

Output?

Page 13: PERL Unit 6 regular expression

Pattern Binding Operator

#!/usr/bin/perl print ("Ask me a question politely:\n");$question = <STDIN>;if ($question =~ /please/i) { print ("Thank you for being polite!\n"); } else { print ("That was not very polite!\n"); }

Page 14: PERL Unit 6 regular expression

Substitution Operator

Replaces a string matched with a regular expression

Default target is $_ The traditional delimeters are /// --

any other single character or two pairs of bracket can be used.

Syntax s/pattern/replacement_string/“s” cannot be omitted

Page 15: PERL Unit 6 regular expression

Substitution Operator

Example s/UNIX/Linux/ $line =~ s/Java/\.Net/ s@/usr/local/bin/perl@/usr/bin/perl@ S{/usr/bin/javac}[/usr/bin/perl]

Page 16: PERL Unit 6 regular expression

Substitution Operator#!/usr/bin/perl $mystring = "Hello world!";print $mystring."\n";$mystring =~ s/world/letran/;print $mystring;

#!/usr/bin/perl $mystring = "Hello World!";print $mystring."\n";$mystring =~ s/world/letran/i;print $mystring;

Page 17: PERL Unit 6 regular expression

Substitution Operator

#!/usr/bin/perl $luv="I love the whole world";print "before substitution: ";print "$luv\n";$luv =~ s/the whole world/the clear blue

skies/ ;print "after the substitution: ";print "$luv\n";

Page 18: PERL Unit 6 regular expression

Substitution Operator

#!/usr/bin/perl $luv="I love the whole world, i love the

clear blue skies";print "before substitution: ";print "$luv\n";$luv =~ s/love/hate/g ;print "after the substitution: ";print "$luv\n";

Page 19: PERL Unit 6 regular expression

Substitution Operator

#!/usr/bin/perl $string = "1 teng 2";$string =~ s/[a-zA-Z]+/$& x 2/e;print "$string“

The e option indicates that the replacement string, $& x 2, is to be treated as an expression

Page 20: PERL Unit 6 regular expression

Substitution Operator

#!/usr/bin/perl $string = "its been 14 years of silence, its

been 14 years of pain";$string =~ s/(\d+)/[$1]/g;print $string;

Page 21: PERL Unit 6 regular expression

Assignment

Create a program that counts the number of occurrences a digit appears in an input then print the sum of all the occurrences of the digit

Create a program that adds 2 to every number in an input.