perl pattern matching operator l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Perl Pattern Matching =~ operator PowerPoint Presentation
Download Presentation
Perl Pattern Matching =~ operator

Loading in 2 Seconds...

play fullscreen
1 / 11

Perl Pattern Matching =~ operator - PowerPoint PPT Presentation


  • 108 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Perl Pattern Matching =~ operator' - wardah


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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 !\n") ;

}

slide2
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”)

}

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

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

slide6
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

slide7
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
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 ?

slide9
(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 ?

slide10
(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 ?

slide11
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?