# File regex1.pl # Regular expressions(regex). String =~ /Pattern/ same as String =~ m/Pattern/ # Match (m) the String with Pattern # Last succesful match. System variables take values: # $` = first part $& = fit $' = last part # position where match is found = length($`) -how many chars are before gives position # Sequence= Too many spaces Pattern=^s before= fit= after= Too many spaces # Sequence= Too many spaces Pattern=^s* before= fit= after=Too many spaces position=0 # Sequence= Too many spaces Pattern=[a-z] before= fit=T after=oo many spaces position=2 # Sequence= Too many spaces Pattern=oo before= T fit=oo after= many spaces position=3 # Sequence= Too many spaces Pattern=[m|n] before= Too fit=m after=any spaces position=6 # Sequence= Too many spaces Pattern=s.*s before= Too many fit=spaces after= position=11 $seq = ' Too many spaces'; #there are 2 spaces before Too # Begins with 1 space $seq =~ m/^\s/; print "\nSequence=$seq Pattern=^\s before=$` fit=$& after=$' " ; # Begins with any number of spaces $seq =~ m/^\s*/; print "\nSequence=$seq Pattern=^\s* before=$` fit=$& after=$' position=", length($`); # Match first letter [a-z] lower case but oignore case (i) $seq =~ m/[a-z]/i; print "\nSequence=$seq Pattern=[a-z] before=$` fit=$& after=$' position=", length($`); # find first oo ignore case $seq =~ m/oo/i; print "\nSequence=$seq Pattern=oo before=$` fit=$& after=$' position=", length($`); # look for first m OR n $seq =~ m/[m|n]/; print "\nSequence=$seq Pattern=[m|n] before=$` fit=$& after=$' position=", length($`); # look for a word that starts with s and ends with s. Any number of chars is .* $seq =~ m/s.*s/; print "\nSequence=$seq Pattern=s.*s before=$` fit=$& after=$' position=", length($`);