regular expression
aaron
asteele at berkeley.edu
Fri Mar 25 21:07:15 EST 2005
dear readers,
given a string, suppose i wanted to do the following:
- replace all periods with colons, except for periods with a digit to
the right and left of it.
for example, given:
'375 mi. south of U.C.B. is 3.4 degrees warmer'
would be changed to:
"375 mi: south of U:C:B: is 3.4 degrees warmer'
i was thinking that a regular expression might do the trick. here's what
i tried:
!----------------------------------------------------------------------!
Python 2.4.1c1
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-49)] on linux2
>>> import re
>>> pattern = re.compile(r'(?!\d)[.](?!\d)')
>>> pattern.sub(':', '375 mi. south of U.C.B is 3.4 degrees warmer.')
'375 mi: south of U:C:B is 3.4 degrees warmer:'
!----------------------------------------------------------------------!
so this works, but not in the following case:
!----------------------------------------------------------------------!
>>> pattern.sub(':', '.3')
'.3'
!----------------------------------------------------------------------!
but going the other direction works:
!----------------------------------------------------------------------!
>>> pattern.sub(':', '3.')
'3:'
!----------------------------------------------------------------------!
any thoughts?
thanks,
aaron
More information about the Python-list
mailing list