Upload
binsent-ribera
View
301
Download
2
Tags:
Embed Size (px)
Citation preview
Regular Expression (cont.)
Perl Programming for Linux Open Systems and the WWW
VSRiveraIBM Learning ServicesWorldwide Certified Manual
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}
Match Operator
Example m/^test(.*) / /^test(.*)/ ; m#test(.*)# m{^ test(.*)} m(^ test(.*)) m>^ test(.*)>
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"; }
Pattern Binding Operator
Specifies the target of a match operation
Syntax $variable =~ /reg_expr/ $variable !~ /reg_expr/
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"; }
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"; }
Pattern Binding Operator
#!/usr/bin/perl -w$ans='x';while ($ans !~/^[YyNn]/){ print "Please enter 'Y'es or 'N'o: ";chomp($ans=<>);}
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
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";}
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
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?
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"); }
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
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]
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;
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";
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";
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
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;
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.