Thursday, Feb 15, 2016
In all your answers, explain what you are thinking so that I can give you partial credit in case you don't get it exactly right. In 2, 3, 4, be careful to use a backslash to “escape” characters that you mean literally, but have special meanings in regex.
import re
some_string = '3009 39 11a493 9 -1.23ax9 -663.9 345.069 20'
myre = '3.{1,2}9'
re.findall(myre,some_string)
Comments:
It returns strings that start with 3 and end in 9, and the strings must contain 3-4 characters.
myre = '\\b[sa][a-z]*t\\b'
some_string = 'It should find only act, at, and sat but not cat or saturn.'
re.findall(myre,some_string)
Comments:
some_string = 'My favorite season is Summer, but Winter is good too. Christmas is fun.'
myre = '\\b[A-Z][a-z]{2,4}[glr]\\b'
re.findall(myre, some_string)
myre = '(\\bSpring\\b|\\bSummer\\b|\\bFall\\b|\\bWinter\\b\\b)'
re.findall( myre, some_string )
Comments:
There are many possibilities here. The second option above is probably better because if the intent is to match only the seasons, then we might as well just list the possibilities since there are only 4 of them. The first option matches some non-existent strings.
some_string = '<img src="picture1.jpg" alt="Mountain View">\nHere is some random sentence.\n<img src="picture2.jpg" width=400>'
myre = '<img src="(.+?)"'
re.findall(myre,some_string)
Comments: