[Tutor] matching a street address with regular expressions

Shawn Milochik Shawn at Milochik.com
Wed Oct 10 19:02:45 CEST 2007


On 10/4/07, Ricardo Aráoz <ricaraoz at gmail.com> wrote:
> Christopher Spears wrote:
> > One of the exercises in Core Python Programming is to
> > create a regular expression that will match a street
> > address.  Here is one of my attempts.
> >
> >>>> street =  "1180 Bordeaux Drive"
> >>>> patt = "\d+ \w+"
> >>>> import re
> >>>> m = re.match(patt, street)
> >>>> if m is not None: m.group()
> > ...
> > '1180 Bordeaux'
> >
> > Obviously, I can just create a pattern "\d+ \w+ \w+".
> > However, the pattern would be useless if I had a
> > street name like 3120 De la Cruz Boulevard.  Any
> > hints?
> >
>

Also, that pattern can be easily modified to have any number of words
at the end:
patt = "\d+ (\w+){1,}"
This would take care of 3120 De la Cruz Boulevard.



More information about the Python-list mailing list