120 likes | 496 Views
Perl Pattern Matching =~ operator. The =~ operator tests whether a pattern appears in a character string. if ( $line =~ /bill/ ) { print(”Hello William !<br>") ; } .
E N D
Perl Pattern Matching=~ operator • The =~ operator tests whether a pattern appears in a character string. if ( $line =~ /bill/ ) { print(”Hello William !\n") ; }
The following sample program reads a question and uses the match operator to establish if the question contains the word 'please'. #!/usr/bin/Perl print(“Polite question ?:\n”); $question = <STDIN>; if($question =~ /please/) { print(“That was polite\n”); } else { print(”That was not polite\n”) }
Perl allows use of regular expressions inconstructing patterns • The special character + means "one or more of the preceding characters." e.g. the pattern /de+f/ matches def, deef, deeef, deeeeeef • The * special character matches zero or more occurrences of the preceding character. • For example, the pattern /de*f/ matches df, def, deef, and so on.
The ? character matches either zero or one occurrence of the preceding character. For example, the pattern /de?f/matches either df or def. Note that it does not match deef, because the character does not match two occurrences of a character.
The ^character matches the start of a line so that the pattern /^the/ matches the word “the” only at the start of a line. The $character matches the end of a line so that the pattern /the$/ matches the word “the” only at the end of a line. The pattern /^$/ matches an empty line.
The Substitute Command: s $line = “the dog and the cat” ; Substitute 1st “the” by “The” $line =~ s/the/The/ ; print ( $line ) ; displays The dog and the cat
General Format of substitution s/pattern/replacement/ and to replace all occurrences of pattern s/pattern/replacement/g $line = “the dog and the cat”; $line =~ s/the/The/g ; print ( $line ) ; displays The dog and The cat
Questions (1) Write a complete program in C and in Perl to prompt the user for a file name and then display the contents of the file. You should check for errors where possible. (2) Which version (C or Perl) should run faster ? Why ?
(3) Assuming speed does not matter, if you were to sell such a program, which one would you choose to sell ? Why ? (4) Which language would you choose to write short programs such as the above ? Why ? (5) What type of applications is Perl suited for in your opinion ? Why ? (6) What type of applications is C suited for in your opinion ? Why ?
(7) A portable program is one that can be moved easily from one computing platform to a different one I.e the program requires few (if any) or minor changes to run on a different platform. (8) Are C programs portable ? What would you have to do with a C program to run it on a different platform ? (9) Are Perl programs portable ? What line might you have to change in a Perl program in porting it to another platform ?
Finally Use the Group Feedback Page for the following questions: Did you find this group activity • Useful • Interesting • Worth using with next years class ? • What word would you use to describe it?