4834_lecture_27_ho

4834_lecture_27_ho - EEL 4834 Lecture 27 EEL 4834 Lecture 27 Regular Expressions 2 Professor O Boykin University of Florida Department of

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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. 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 junk = [ (x,...
View Full Document

This note was uploaded on 02/11/2010 for the course EEL 4834 taught by Professor Staff during the Fall '08 term at University of Florida.

Page1 / 8

4834_lecture_27_ho - EEL 4834 Lecture 27 EEL 4834 Lecture 27 Regular Expressions 2 Professor O Boykin University of Florida Department of

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online