0% found this document useful (0 votes)
48 views

Fall Semester 2021-2022 Scripting Languages For VLSI Design Automation LAB - 05 - PERL Regular Expression

This document provides instructions for a lab on using regular expressions in Perl. It includes examples of searching for patterns in strings using regular expressions and describes common regular expression patterns, abbreviations, anchors and multipliers. The lab has students create Perl scripts that demonstrate pattern matching, remembering and recalling patterns, and opening, reading from and writing to files.

Uploaded by

Bhargavi Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Fall Semester 2021-2022 Scripting Languages For VLSI Design Automation LAB - 05 - PERL Regular Expression

This document provides instructions for a lab on using regular expressions in Perl. It includes examples of searching for patterns in strings using regular expressions and describes common regular expression patterns, abbreviations, anchors and multipliers. The lab has students create Perl scripts that demonstrate pattern matching, remembering and recalling patterns, and opening, reading from and writing to files.

Uploaded by

Bhargavi Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Fall Semester 2021-2022

Scripting Languages for VLSI Design Automation

LAB - 05 - PERL Regular Expression


This lab has few simple examples of regular expression which will help you to get
familiar with the usage of regular expressions. A regular expression, often called a pattern in
Perl may match infinite number of possible text strings. Regular expression is also a pattern
written between a pair of forward slashes (/ /). This pattern can be used to identify the
strings of text of interest match with the contents of a variable or $_.

Step 1: Navigate to PERL_Practice_Lab

cd ./Scripting/Perl/PERL_Practice_Lab

Step 2: In PERL_Practice_Lab directory, create a file named reg_exp.pl

gedit reg_exp.pl

Step 3: Type the following contents to the file reg_exp.pl

#!/usr/bin/perl

#Following lines of code shows to search the pattern every in the variable $_

$_ = "Hello Good Morning everybody\n";

if( $_ =~ /every/) {

print "Found the word every\n";

#Following line shows to search any numerical pattern in the variable $string

$string = "Some string 12345";

if($string =~ /[123456789]/) {

Scripting Languages for VLSI Design Automation


print "Number found\n";

# Following script shows pattern matching with pattern abbreviationsand multipliers.

#It also shows how to memorize and recall the memorized pattern

$a = "Hello Good morning Hello";

if($a =~ /(\w+)\s(\w+)\s(\w+)\s\1/) {

print "$1 $2 $3\n";

} else {

print "Does not match\n";

Step 4: Save the contents and execute the script from the terminal to see the output.

perl reg_exp.pl

Note: Try to use and get familiar with the following pattern abbreviation, anchors and
multipliers.

Pattern Abbreviations:

Pattern Abbreviations Explanation


. Any character except newline
\d A digit same as [0-9]
\w A word character, same as [A-Z, a-z, 0-9]
\s A space character (tab, space, etc)
\D Not a digit character, same as [^0-9]
\W Not a word character
\S Not a space character

Scripting Languages for VLSI Design Automation


Pattern anchors:

Anchor Meaning
^ Anchors to the beginning of a string
$ Anchors to the end of a string
\b Anchors to a word boundary

Pattern Multipliers:

Symbol Meaning

* Find zero or more occurrences

+ Find one or more occurrences

? Find zero or one occurrence

Scripting Languages for VLSI Design Automation


File Handling

This section contains an example script to show how to open, read and write to a file.
A file can be opened using the open keyword and can be closed using the close keyword.
There are three modes of opening the file viz., read mode, write mode and append mode.

MODE OPERAND
Read <
Write >
Append >>

Step1: Create a file named file_handling.pl in the PERL_Practice_Lab directory

gedit file_handling.pl

Step2: Type the following contents in the created file

#Following script shows how to open and read the contents of a file

open FH1, "<reg_exp.pl";

while (<FH1>) {

print “$_”;

print “End of File reached\n”;

close (FH1);

#Following script shows how to open, read and to write to a file

open FH1, "<reg_exp.pl";

open FH2, ">reg_exp_copy.pl";

while (<FH1>) {

print FH2 “$_”;

print “End of File Reached…written the contents of one file to the other file\n”;

close (FH1);

close (FH2);
Scripting Languages for VLSI Design Automation
Step 3: Save and execute the script.

Step 4: Analyze the output.

Scripting Languages for VLSI Design Automation

You might also like