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

pract 6

The document outlines practical exercises using regular expressions in Perl, focusing on match, substitution, and transliteration operators. It includes code examples for matching DNA and RNA sequences, replacing patterns, and removing duplicate characters. The conclusion emphasizes the effectiveness of regex in enhancing text manipulation capabilities in Perl scripting.

Uploaded by

Aditya Khabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

pract 6

The document outlines practical exercises using regular expressions in Perl, focusing on match, substitution, and transliteration operators. It includes code examples for matching DNA and RNA sequences, replacing patterns, and removing duplicate characters. The conclusion emphasizes the effectiveness of regex in enhancing text manipulation capabilities in Perl scripting.

Uploaded by

Aditya Khabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DATE: / /2025

PRACTICAL-6
Regular Expressions

AIM: To perform programs on regular expressions in Perl programming language.

INTRODUCTION: A regular expression is a string of characters that defines the pattern or


patterns you are viewing. Regular Expression (Regex or RE) in Perl is a special text string for
describing a search pattern within a given text. The basic method for applying a regular
expression is to use the pattern binding operator ‘=~’. The left-hand side of the statement will
contain a string which will be matched with the right-hand side containing the specified pattern.
There are three regular expression operators within Perl:
• Match Regular Expression - m//
• Substitute Regular Expression - s///
• Transliterate Regular Expression - tr///

➢ Match Operator - The match operator “m//” is used to match a string or a statement
against a regular expression. The forward slash used in the operator (m//) acts as the
delimiter and this delimiter can also be like m{}, m(). The expression is written in
between two forward slashes used in the operator.
Syntax: m/PATTERN/

Match Operator Modifiers:

The match operator supports its own set of modifiers. The /g modifier allows for
global matching. The /i modifier will make the match case insensitive.

Sr. No. Modifier & Description


1. I
Makes the match case insensitive.
2. m
Specifies that if the string has newline or carriage return characters, the ^ and $
operators will now match against a newline boundary, instead of a string boundary.
3. o
Evaluates the expression only once.
4. x
Allows you to use white space in the expression for clarity.
5. g
Globally finds all matches.

41
6. cg
Allows the search to continue even after a global match fails.

➢ Substitution Operator - The substitute operator “s///” is used to search a specific word
and then replace it with a given regular expression. The forward slash used in the
operator (s///) acts as the delimiter.
Syntax: s/PATTERN/REPLACEMENT/;

➢ Transliterate Operator - The translation or transliteration operator “tr///” is used to


replace all the occurrences of a character with a given single character. The forward
slash used in the operator (tr///) acts as the delimiter.
Syntax: tr/SEARCHLIST/REPLACEMENTLIST/;

Sr. No. Modifier & Description


1. c
Complements SEARCHLIST.
2. s
Squashes duplicate replaced characters.

Q1. Write a Perl to accept a DNA sequence and match against pattern “aatg” is found
on entered sequence and check whether the match case insensitive.

CODE:
print"Enter a DNA
sequence: "; my
$seq=<stdin>;
chomp($seq);
if($seq=~/aatg/i){
print"The pattern 'aatg' is found in the sequence\n"
}else{

print"The pattern 'aatg' is not found in the sequence\n"

Fig 1.1. Match not found against pattern “aatg”.

42
Fig 1.1. Match found against pattern “aatg”.

Q2. Write a Perl script to accept DNA sequence from user and search pattern “att” and
replace with “agt”.

CODE:
print"Enter a DNA sequence\n";
chomp($DNA=<stdin>);
$DNA=~s/att/agt/g;
print"Modified
sequence:\n";
print"$DNA\n";

OUTPUT:

Fig 2. To search pattern “att” and replace with “agt”.

Q3. Write a Perl script to accept RNA sequence from user and search pattern “auu”
and replace with “agcu” in whole sequence.

CODE:
print "Enter an RNA sequence:
"; my $rna_sequence =
<STDIN>; chomp
$rna_sequence;
$rna_sequence =~ s/auu/agcu/g;
print "Modified RNA sequence: $rna_sequence\n";

43
OUTPUT:

Fig 3. To search pattern “auu” and replace with

“agcu”.

Q4. Write a Perl script to accept RNA sequence and convert that into DNA sequence.
CODE:
print "Enter an RNA sequence:
"; my $rna_sequence =
<STDIN>;
$rna_sequence =~ tr/a,u,g,c/A,U,G,C/;
$rna_sequence =~ tr/U/T/;
print "DNA Sequence:

$rna_sequence\n";

OUTPUT:

Fig 4. To convert RNA sequence into DNA sequence.

Q5. Write a Perl script to accept a string and remove duplicate characters from
entered string.

CODE:
print "Enter an DNA sequence: ";
$dna_sequence = <STDIN>;
$dna_sequence=~tr/a-z/a-z/s;
print"String with duplicate

removed:$dna_sequence\n";

44
OUTPUT:

Fig 5. To remove duplicate characters from entered


string.

Result:
Perl programs were successfully implemented using regular expression operators, including
match, substitution, and translation operators. These operators enabled efficient pattern
matching, text manipulation, and transformation, demonstrating the powerful string-processing
capabilities of Perl.

Conclusion:
The implementation of Perl programs with regular expressions provided practical insights into
pattern matching and text processing. Utilizing regex operators enhances the flexibility and
efficiency of text manipulation in Perl scripting.

References:
1. GeeksforGeeks. (2019, May 30). Perl | Regular Expressions. GeeksforGeeks.
https://www.geeksforgeeks.org/perl-regular-expressions/
2. TutorialsPoint. (n.d.). Perl - Regular Expressions. TutorialsPoint.
https://www.tutorialspoint.com/perl/perl_regular_expressions.htm
3. Perldoc. (n.d.). perlretut - Perl regular expressions tutorial. Perldoc Browser.
https://perldoc.perl.org/perlretut

45

You might also like