Regex replacement operation

David K. Trudgett dkt at registriesltd.com.au
Thu Jan 16 17:21:55 EST 2003


Thanks, Cliff, I didn't expect to be spoon fed like that! You've
spoilt me now (but saved me some time!).

On Wednesday 2003-01-15 at 22:29:21 -0800, Cliff Wells wrote:

> Anyway, here are two different (yet similar) implementations.  The
> second is mostly to demonstrate some useful features of the re module
> and Python string interpolation.  I'm sure it can be done better :P

Maybe, but not by me! I like the first example for its conciseness. I
also like the "int(d) for d in subdate" bit. That line didn't even
parse in Python 1.5.2, so it must be a relatively new addition.

The second example with named groups is quite interesting. Perl
doesn't even have that (probably will in Perl 6, though, because
they're taking the opportunity to overall Perl's regex syntax, which
sort of grew into a bit of a tangled web over the years).

> 
> 
> # ===== Example 1 ======
> 
> import re
> 
> s = 'Today is 16-1-2003 or 16-01-2003. New Year was 1-1-2003, reportedly.'
> 
> rexp = re.compile('(\d\d?-\d\d?-\d\d\d\d)')
> 
> for date in rexp.findall(s):
>     subdate = date.split('-')
>     subdate.reverse()
>     subdate = "%d-%02d-%02d" % tuple([int(d) for d in subdate])
>     s = re.sub(date, subdate, s)
> 
> print s
> 
> 
> # ===== Example 2 ======
> 
> 
> s = 'Today is 16-1-2003 or 16-01-2003. New Year was 1-1-2003, reportedly.'
> 
> rexp = re.compile('(?P<date>(?P<day>\d\d?)-(?P<month>\d\d?)-(?P<year>\d\d\d\d))')
> 
> for match in rexp.finditer(s):
>     groupdict = match.groupdict()
>     for i in groupdict:
>         try:
>             groupdict[i] = int(groupdict[i])
>         except ValueError: # date key isn't a valid int
>             pass
>     subdate = "%(year)d-%(month)02d-%(day)02d" % groupdict
>     s = re.sub(groupdict['date'], subdate, s)
> 
> print s

Thanks again for your time. I know stuff like that doesn't get put
together in five minutes.

Bye for now.

David Trudgett








More information about the Python-list mailing list