Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Regular Expression A regular expression is a template that either matches or doesn’t match a given string."— Presentation transcript:

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

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

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

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

5 Regular Expression: substitutions 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!

6 Regular Expression: translations 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.

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

8 Regular Expression 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


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

Similar presentations


Ads by Google