pract 6
pract 6
PRACTICAL-6
Regular Expressions
➢ 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/
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.
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/;
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{
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:
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:
“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:
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:
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