Professional Documents
Culture Documents
REGULAR EXPRESSIONS
C. Prayline Rajabai
Assistant Professor
SENSE
VIT University
HANDLING FILES IN PERL
Handling files in PERL is simple.
2
OPENING A FILE
Open command opens a file and returns a filehandle
Syntax :
while(<ABC>) {
4
WRITING TO A FILE
$filename = /home/prayline/report.txt;
for $i (1..20) {
5
CLOSING A FILE
close command in perl is used to close the file.
Syntax :
close FILEHANDLE;
6
COPYING A FILE
The below script shows how to copy the contents
of one file to the other.
#!/usr/bin/perl
open(DATA1, "<file1.txt");
open(DATA2, ">file2.txt");
# Copy data from one file to another.
while(<DATA1>) {
print DATA2 $_;
}
close( DATA1 );
close( DATA2 );
7
RENAMING AND DELETING A FILE
Rename command is used to rename a file.
unlink ("/usr/test/file1.txt");
8
REGULAR EXPRESSIONS
Regular expression is one of the most powerful
features in Perl.
$found = 0;
$_ =Hello good morning everybody;
$search = good;
foreach $word (split) {
if ($word eq $search) {
$found = 1;
last;
}
}
if($found) {
Print Found the word every\n;
10
PERL SCRIPT TO SEARCH A WORD USING REGEX
$_ = Hello Good Morning everybody\n
if( $_ =~ /every/) {
print Found the word every\n;
}
Substitute - s///
Transliterate - tr///
12
THE MATCH OPERATOR
The match operator, m// is used to match a string or
You can omit m from m// if the delimiters are forward slashes,
but for all other delimiters you must use the m prefix.
13
THE MATCH OPERATOR
Example :
$search = Welcome to VIT";
if ($search =~ /VIT/){
print $search is found in the string\n";
}else{
print $search is not found in the string\n";
}
14
CHARACTER CLASS
Use square brackets to specify any value in the list of
possible values
Example :
my $string = Some string 12345;
if($string =~ /[123456789]/) {
Print Found a number\n;
}
if($string =~ /[aeiou]/) {
print Found a vowel\n;
}
if($string =~ /[0123456789ABCDEF]/) {
print Found a hex digit\n;
15
}
CHARACTER CLASS NEGATION
Use ^ at the beginning of the character class to specify any
single character that is not one of these values
Example :
my $string = Some string 12345;
if($string =~ /[^123456789]/) {
print Number not found\n;
}
if($string =~ /[^aeiou]/) {
print Vowel not found\n;
}
if($string =~ /[^0123456789ABCDEF]/) {
print Hex digit not found\n;
16
}
PATTERN ABBREVIATIONS
Pattern Explanation
Abbrevations
18
ANCHORS
There are three ways to define an anchor.
Anchor Meaning
^ Anchors to the beginning of a string
$ Anchors to the end of a string
\b Anchors to a word boundary
Examples
if($string =~ /^\w/)
Meaning :: does the string start with a word character.
if($string =~ /\d$/)
Meaning :: does the string end with a digit.
if($string =~ /\bGood\b/)
19
Meaning :: does the string contain the word Good.
MULTIPLIERS
There are three multiplier characters.
Symbol Meaning
* Find zero or more occurrences
+ Find one or more occurrences
? Find zero or one occurrence
Examples
$string =~ /^\w+/;
$string =~ /\d?/;
$string =~ /\d\w+\s+/;
$string =~ /\w+\s?$/;
20
MEMORY IN REGEX
We can use parentheses to capture a piece of matched text
for future use.
Examples
$string = Cat and Rat;
$string =~ /(\w+)\s/;
print $1\n;
21
#prints Cat\n
THE SUBSTITUTION OPERATOR
Used to replace a substring by another substring
Syntax :
$new =~ s/pattern_to_match/new_pattern/;
It looks for the pattern once and only the first occurrence
is replaced.
22
SUBSTITUTION EXAMPLE
#/user/bin/perl
$string = "The cat sat on the mat";
$string =~ s/cat/dog/;
print "$string\n";
23
MODIFIERS
Two types of modifiers are defined.
/i :: Ignore case
Examples
$string = Cat and Rat;
if($string =~ s/t/n/g) {
print $string;
}
if($string =~ /CAT/i) {
print Cat is present in the string\n;
} 24
TRANSLATION OPERATOR
It is similar to substitution
y/SEARCHLIST/REPLACEMENTLIST/
25
TRANSLATION OPERATOR
Standard Perl ranges can also be used, allowing you to specify
ranges of characters either by letter or numerical value.
$string =~ tr/a-z/A-Z/;
Examples
#/user/bin/perl
$string = 'The cat sat on the mat';
$string =~ tr/a/o/;
print "$string\n";
#prints The cot sot on the mot 26