ENG 06: Engineering Problem Solving with MATLAB – Lecture 17 notes

4 Pages
Unlock Document

University of California - Davis
Professor Rajeevan Amirtharajah

ENG 06 – Lecture 17; 3/5/2013 Notes: Pattern Matching  Matching Any Character: Use dot. o Identifies any single character, including white space o Ex. str = ‘The rain in Spain falls mainly on the plain.’;  Locate sequences of 5 consecutive characters that end in ‘ain’  Expr = ‘..ain’;  startIndex = regexp(str,exp)  startIndex = 4 13 24 39  regexp(str,’..ain’,’match’)  ‘rain’ ‘Spain’ ‘main’ ‘plain’  Selected characters [c1c2c3] o Matches any character contained within brackets  [c1-c2] o matches any character in the range of c1 through c2  str = 'The rain in Spain falls mainly on the plain.';  
 >> [mat idx] = regexp(str, '[A‐ Z]\w*', 'match', 'start')  mat =
 'The' 'Spain'  idx =
 1 13  \s any white space character o str = 'The rain in Spain falls mainly on the plain.'; o >>[mat ix1 ix2] = regexp(str, '\w*n\s', 'match', 'start', 'end') mat = o 'rain ' 'in ' 'Spain ' ix1 = o 'on '  find all words starting with lower case m or capital M and ending with y  str = 'My flowers may bloom in May';  >> [a,b]=regexpi(str, 'm\w*y','match')  %look for 0 or 1 occurrences in between m and y using *  a=  'My' 'may' 'May'  b=  1 12 25  Extract all words o str='Romeo and Juliet';
 o >> regexpi(str, '\w+','match') o ans = o 'Romeo' 'and' 'Juliet'  Split a sentence at any white space character o Divide up words using ‘split’  str='Madrid, Spain Africa';  >> [dd]=regexpi(str, '\s','split')  dd =
 'Madrid,' 'Spain' 'Africa'  each d  Metacharacter \ inhibits specialness of a meta character o Interprets \s as white space metacharacter o Put in character for literal backslash (2 consecutive backslashes) o Ex:  quote1 = 'He\she is hardly working hard. ';  regexpi(quote1, 'He\she', 'match')  ans = {}  Trial>> regexpi(quote1, 'He\\she', 'match')  ans = 'He\she’ o Similarly, if you try to match with . it matches anything o Again, need the backslash: \. to match period o Ex.  Trial>> regexpi(quote1, 'hard.', 'match')  ans = 'hardl' 'hard.'  No backslash  Trial>> regexpi(quote1, 'hard\.', 'match')  ans = 'hard.'  Uses backslash  Grouping/Position metacharacter o Specify that some characters should be repeated:  If you want to match 5 b’s, group using parentheses (B){5}  {5} means it must match exactly 5  Greedy match o Quote with a lot of repetitions, a match would give a lot of possible answers  quote1 = 'I''m gonna make this pencil disappear...Ta‐ daa! It''s... it''s gone.’;  match all Is with apostrophes  aa=regexpi(quote1, 'I''.*\w', 'match')  aa =  'I'm gonna make this pencil disappe
More Less

Related notes for ENG 6

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.