# File regex0.pl # Regular expressions(regex). String =~ m/Pattern/ same as String =~ /Pattern/ # Match (m) the String with Pattern # Last succesful match. System variables take values: # $` = first part (' quote) # $& = fit # $' = last part (` backquote) # position where match is found = length($`) - # how many chars are before gives position # C:\myPerl>perl regex0.pl # Sequence=Blaise2012 Pattern=^Bla before= fit=Bla after=ise2012 # Sequence=Blaise2012 Pattern=2012 before=Blaise fit=2012 after= # Position in sequence =6 # Sequence=Blaise2012 Pattern=[0-9] before=Blaise fit=2 after=012 position=6 # Sequence=Blaise2012 Pattern=b before= fit=B after=laise2012 position=0 # Sequence=Blaise2012 Pattern=2..2 before=Blaise fit=2012 after= position=6 # Sequence=Blaise2012 Pattern=[0-9].2 before=Blaise2 fit=012 after= position=7 # no match $seq = 'Blaise2012'; # match with pattern starting(^) with Bla: before=(nothing) fit=Bla after=ise 2012 $seq =~ m/^Bla/; print "\nSequence=$seq Pattern=^Bla before=$` fit=$& after=$' " ; # match with pattern: find anywhere 2012: before=Blaise fit=2012 after= (nothing) $seq =~ m/2012/; print "\nSequence=$seq Pattern=2012 before=$` fit=$& after=$' " ; print "\nPosition in sequence =", length($`); # Match first digit. [0-9] means any digit. Finds first 2 position=6 $seq =~ m/[0-9]/; print "\nSequence=$seq Pattern=[0-9] before=$` fit=$& after=$' position=", length($`); # find first B ignore case $seq =~ m/b/ig; print "\nSequence=$seq Pattern=b before=$` fit=$& after=$' position=", length($`); # look for a 4 length sequence that starts with 2 and ends with 2 $seq =~ m/2..2/; print "\nSequence=$seq Pattern=2..2 before=$` fit=$& after=$' position=", length($`); # look for a 3 length sequence that starts with a digit and ends with 2 $seq =~ m/[0-9].2/; print "\nSequence=$seq Pattern=[0-9].2 before=$` fit=$& after=$' position=", length($`); # find first HAPPY ignore case. No match! # carefull! variables $`,$',$& detain the last values for succes! # you have to always check before if match works when finding position. if ($seq =~ m/HAPPY/) {print "\n match";} else {print "\n no match";} exit;