Perl - Part Iii: Indian Institute of Technology Kharagpur
Perl - Part Iii: Indian Institute of Technology Kharagpur
1
String Functions
2
• Another example:
$new = join ' ', $x1, $x2, $x3, $x4, $x5, $x6;
$sep = ‘::’;
$new = join $sep, $x1, $x2, $w3, @abc, $x4, $x5;
3
Regular Expressions
Introduction
4
An Example: without RegEx
$found = 0;
$_ = “Hello good morning everybody”;
$search = “every”;
foreach $word (split) {
if ($word eq $search) {
$found = 1;
last;
}
}
if ($found) {
print “Found the word ‘every’ \n”;
}
Using RegEx
if ($_ =~ /every/) {
print “Found the word ‘every’ \n”;
}
5
• The previous example illustrates
literal texts as regular expressions.
¾Simplest form of regular expression.
• Point to remember:
¾When performing the matching, all the
characters in the string are considered
to be significant, including punctuation
and white spaces.
For example, /every / will not match in the
previous example.
if (/IIT K/) {
print “’IIT K’ is present in the string\n”;
{
if (/Kharagpur students/) {
print “This will not match\n”;
}
6
Types of RegEx
Matching
7
The =~ Operator
Examples
if ($string =~ m/day/) {
print “Match successful \n";
}
if ($string =~ /day/) {
print “Match successful \n";
}
8
$string = “Good day”;
if ($string =~ m@day@) {
print “Match successful \n";
}
if ($string =~ m[day[ ) {
print “Match successful \n";
}
Character Class
9
Character Class Negation
Pattern Abbreviations
10
$string = “Good and bad days";
if ($string =~ /d..s/) {
print "Found something like days\n";
}
if ($string =~ /\w\w\w\w\s/) {
print "Found a four-letter word!\n";
}
Anchors
11
if ($string =~ /^\w/)
:: does string start with a word character?
if ($string =~ /\d$/)
:: does string end with a digit?
if ($string =~ /\bGood\b/)
:: Does string contain the word “Good”?
Multipliers
12
Substitution
Basic Usage
13
Examples
$xyz =~ s/Lakshman/Bharat/;
$xyz =~ s/R\w+a/Bharat/;
$xyz =~ s/[aeiou]/i/;
$abc =~ s/\d+/12/;
$abc =~ s /\n$/ /;
Common Modifiers
$string =~ s/m/j/g;
# Ram -> Raj, Shyam -> Shyaj
14
Use of Memory in RegEx
Examples
$string =~ /^(\w+)/;
print $1, "\n"; # prints “Ra\n”
$string =~ /(\w+)$/;
print $1, "\n"; # prints “st\n”
$string =~ /^(\w+)\s+(\w+)/;
print "$1 $2\n";
# prints “Ramnd Shyam are honest”;
15
$string = “Ram and Shyam are very poor";
if ($string =~ /(\w)\1/) {
print "found 2 in a row\n";
}
if ($string =~ /(\w+).*\1/) {
print "found repeat\n";
}
Example 1
if ($age =~ /\D/) {
print "$age is a non-number!\n";
}
16
Example 2: validation contd.
17
$&, $` and $’
• What is $&?
¾It represents the string matched by the
last successful pattern match.
• What is $`?
¾It represents the string preceding
whatever was matched by the last
successful pattern match.
• What is $‘?
¾It represents the string following whatever
was matched by the last successful
pattern match .
¾Example:
$_ = 'abcdefghi';
/def/;
print "$\`:$&:$'\n";
# prints abc:def:ghi
18
• So actually ….
¾S` represents pre match
¾$& represents present match
¾$’ represents post match
19
SOLUTIONS TO QUIZ
QUESTIONS ON
LECTURE 22
20
Quiz Solutions on Lecture 22
while <INP> {
print OUT “$. : $_”;
}
close INP;
close OUT;
21
Quiz Solutions on Lecture 22
5. How does Perl check if the result of a
relational expression is TRUE of FALSE.
Only the values 0, undef and empty string
are considered as FALSE. All else is
TRUE.
22
QUIZ QUESTIONS ON
LECTURE 23
23
Quiz Questions on Lecture 23
24