Regular Expression A regular expression is a template

  • Slides: 8
Download presentation
Regular Expression A regular expression is a template that either matches or doesn’t match

Regular Expression A regular expression is a template that either matches or doesn’t match a given string.

Regular Expression: simple patterns $_ = “yabba dabba”; #pattern match return a true or

Regular Expression: simple patterns $_ = “yabba dabba”; #pattern match return a true or false if($_ =~ /abba/) { print “It matched!n” }

Regular Expression: v Dot (. ): matches any single character(but not n) v v

Regular Expression: v Dot (. ): matches any single character(but not n) v v Backslash(): escape from special character v v v /3. 14159/ matches 3. 14159, 3214159, 3=14159 Star (*): matches zero or more of preceding item v /fred*/ matches fre, fredddd. Plus(+): matches one or more of preceding item v v /3. 14159/ matches 3. 14159 only /fred+/ matches fred, fredddd but not fre Square brackets([ ]) matches any single character from within the class v [a-z. A-Z] matches any letter [0 -9] matches any number

Regular Expression: v Parentheses( “()”) may be used for grouping v v Vertical bar

Regular Expression: v Parentheses( “()”) may be used for grouping v v Vertical bar (|) matches either the left side or and right side v v $test = “apple banana”; if($test =~/(ap*le)s+(banana)/) { print “first group: $1 and second group: $2n”; } $test 1 = “ 1 abc”; $test 2 = “ 2 abc”; if(($test 1 =~/(1|2)abc/)and($test 2 =~/(1|2)abc/)) { print “test 1 and test 2 both match to our pattern”; } Anchors (^) matches the beginning of a string(line) and ($) match the end of a string (line)

Regular Expression: substitutions n Perl’s s/// substitution operator does the ‘search and replace’ $line

Regular Expression: substitutions n Perl’s s/// substitution operator does the ‘search and replace’ $line = “He is out with Barney. He is really happy!”; $line =~ s/Barney/Fred/; #He is out with Fred. He is really happy! $line =~ s/Barney/Wilma/; #He is out with Fred. He is really happy! (nothing happens as search failed) $line =~ s/He/She/; #She is out with Fred. He is really happy! $line = “He is out with Fred. He is really happy” $line =~ s/He/She/g; #She is out with Fred. She is really happy!

Regular Expression: translations n The "tr///" operator performs a substitution on the individual characters

Regular Expression: translations n The "tr///" operator performs a substitution on the individual characters ina string. Examples: $x =~ tr/a/b/; # Replace each "a" with a "b". $x =~ tr/ /_/; # Convert spaces to underlines. $x =~ tr/aeiou/AEIOU/; # Capitalise vowels. $x =~ tr/0 -9/QERTYUIOPX/; # Digits to letters. $x =~ tr/A-Z/a-z/; # Convert to lowercase.

Regular Expression: split & join n Split breaks up a string according to a

Regular Expression: split & join n Split breaks up a string according to a separator. $line = “abc: def: g: h”; @fields = split(/: /, $line) #(‘abc’, ’def’, ’g’, h’) n n Join glues together a bunch of pieces to make a string. $new_line = join(“: ”, @fields) #“abc: def: g: h”

Regular Expression n Write a function that transcribe DNA to RNA Write a function

Regular Expression n Write a function that transcribe DNA to RNA Write a function that do reverse complement of strand of DNA. Test these two functions and print output into a report file