{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

4834_lecture_27_ho

# 4834_lecture_27_ho - EEL 4834 Lecture 27 EEL 4834 Lecture...

This preview shows pages 1–3. Sign up to view the full content.

EEL 4834: Lecture 27 EEL 4834: Lecture 27 Regular Expressions 2 Professor O. Boykin University of Florida, Department of Electrical and Computer Engineering November 5, 2009 EEL 4834: Lecture 27 Today: Review REs for integers and/or decimals Learn how to count groups. Do a practice problem.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
EEL 4834: Lecture 27 Groups in Regular Expressions Using Groups to Extract Parts of a Match The re.search function returns a match object. It can tell us what parts of the original string match groups of our regular expression. match.group(0) is the substring that matches the entire regular expression. match.group(i) contains the part that matches the i th parenthesis (group) in the regular expression. Consider the example of parsing dates: import re dates = [ "06/09/1974" , "01/01/1901" , "07/04/1776" , "2/14/2008" , "junk" ] regexp = r ’( \ d { 1,2 } )/( \ d { 1,2 } )/( \ d { 4 } )’ for date in dates: m = re.search(regexp, date) if m: month = int(m.group(1)) day = int(m.group(2)) year = int(m.group(3)) print "Month: %i Day: %i Year: %i" % (month, day, year) else: print "No match for: %s" % date EEL 4834: Lecture 27 Numbers Matching Positive or Negative Integers The following code can extract integers from strings: import re, random regexp = r ? \ d+’ accept = [ str(random.randint( 10000,10000)) for x in xrange(10000) ] #group(0) holds the substring string that matches the regexp for v in accept: assert ( int(re.search(regexp,v).group(0)) == int(v) ) with
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}