1 / 11

Perl Pattern Matching =~ operator

Perl Pattern Matching =~ operator. The =~ operator tests whether a pattern appears in a character string. if ( $line =~ /bill/ ) { print(”Hello William !<br>&quot;) ; } .

wardah
Download Presentation

Perl Pattern Matching =~ operator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl Pattern Matching=~ operator • The =~ operator tests whether a pattern appears in a character string. if ( $line =~ /bill/ ) { print(”Hello William !\n") ; }

  2. 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”) }

  3. 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.

  4. 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.

  5. 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.

  6. 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

  7. 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

  8. 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 ?

  9. (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 ?

  10. (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 ?

  11. 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?

More Related