[10/19/2011] Quiz #3 on table -- please pick up Quiz #4 today (list comprehensions and regular expressions) ========================================================================== \(?\d{3}[\)\-]?\d{3}\-?\d{4} the \(? matches zero or one open-parens next, the \d{3} matches 3 digit characters [0-9] next, the [\)\-]? matches zero or one of a single char from ) or - next, the \d{3} matches 3 digit characters [0-9] next, the \-? matches zero or one - characters next, the \d{4} matches 4 digit characters [0-9] [a-z ] [a-z\s] \s*\d{3}\s*\d{3}\s*\d{4} match abxx or cdxx (ab|cd)xx David E. Goldschmidt, Ph.D. David E Goldschmidt Ph D David E Goldschmidt PhD i.b.m. ==> ibm or i b m r2-d2 ==> r2d2 or r2 d2 ========================================================================== Python 3.2 (r32:88445, Mar 25 2011, 19:28:28) [GCC 4.5.2] on linux2 Type "copyright", "credits" or "license()" for more information. ==== No Subprocess ==== >>> name = 'monty python' >>> name[8:5:-1] + name[3::-2] 'typto' >>> sentence = 'the quick brown fox jumps over the lazy dog' >>> sentence.split( ' ' ) ['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'] >>> sentence.split( ' ' )[2] 'brown' >>> sentence.split( ' ' )[3] 'fox' >>> sentence.split( ' ' )[4] 'jumps' >>> sentence 'the quick brown fox jumps over the lazy dog' >>> sentence.replace( 'the', 'a' ).replace( 'quick ', '' ) 'a brown fox jumps over a lazy dog' >>> >>> >>> >>> sentence.replace( 'the', 'a' ).replace( 'lazy ', '' ) 'a quick brown fox jumps over a dog' >>> sentence.replace( 'the', 'a' ).replace( 'brown ', '' ) 'a quick fox jumps over a lazy dog' >>> >>> sentence 'the quick brown fox jumps over the lazy dog' >>> >>> >>> >>> import re >>> >>> re.sub( '\w+o\w+', '', sentence ) 'the quick jumps over the lazy ' >>> >>> sentence 'the quick brown fox jumps over the lazy dog' >>> >>> sentence 'the quick brown fox jumps over the lazy dog' >>> number = '518-276-6000 ext.1234' >>> m = re.match( '\(?\d{3}[\)\-]?\d{3}\-?\d{4}', number ) >>> if m: m.group( 0 ) '518-276-6000' >>> >>> >>> m.group( 1 ) Traceback (most recent call last): File "", line 1, in m.group( 1 ) IndexError: no such group >>> m = re.match( '\(?(\d{3})[\)\-]?\d{3}\-?\d{4}', number ) >>> if m: m.group( 0 ) m.group( 1 ) '518-276-6000' '518' >>> m = re.match( '\(?(\d{3})[\)\-]?(\d{3}\-?(\d{4}))', number ) >>> if m: m.group( 0 ) m.group( 1 ) m.group( 2 ) m.group( 3 ) '518-276-6000' '518' '276-6000' '6000' >>> Enter number: 16 p1 search() yes! p2 search() yes: efgh5678 8 20 >>> Enter number: 7 p1 search() yes! p2 match() yes: abcd123 0 11 p2 search() yes: abcd123 0 11 >>> Enter number: 21 p1 search() yes! >>> Enter number: 14 p1 search() yes! p2 search() yes: efgh56 8 18 >>> Enter number: 5 p1 search() yes! p2 match() yes: abcd1 0 9 p2 search() yes: abcd1 0 9 >>> Enter number: 20 p1 search() yes! >>> Enter number: 15 p1 search() yes! p2 search() yes: efgh567 8 19 >>> Enter number: 6 p1 search() yes! p2 match() yes: abcd12 0 10 p2 search() yes: abcd12 0 10 >>> Enter number: 19 p1 search() yes! >>> >>>