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[color=blue][color=green][color=darkred]
>>> import re
>>> pattern = re.compile(r'(? !\d)[.](?!\d)')
>>> pattern.sub(':' , '375 mi. south of U.C.B is 3.4 degrees warmer.')[/color][/color][/color]
'375 mi: south of U:C:B is 3.4 degrees warmer:'
!----------------------------------------------------------------------!
so this works, but not in the following case:
!----------------------------------------------------------------------![color=blue][color=green][color=darkred]
>>> pattern.sub(':' , '.3')[/color][/color][/color]
'.3'
!----------------------------------------------------------------------!
but going the other direction works:
!----------------------------------------------------------------------![color=blue][color=green][color=darkred]
>>> pattern.sub(':' , '3.')[/color][/color][/color]
'3:'
!----------------------------------------------------------------------!
any thoughts?
thanks,
aaron
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[color=blue][color=green][color=darkred]
>>> import re
>>> pattern = re.compile(r'(? !\d)[.](?!\d)')
>>> pattern.sub(':' , '375 mi. south of U.C.B is 3.4 degrees warmer.')[/color][/color][/color]
'375 mi: south of U:C:B is 3.4 degrees warmer:'
!----------------------------------------------------------------------!
so this works, but not in the following case:
!----------------------------------------------------------------------![color=blue][color=green][color=darkred]
>>> pattern.sub(':' , '.3')[/color][/color][/color]
'.3'
!----------------------------------------------------------------------!
but going the other direction works:
!----------------------------------------------------------------------![color=blue][color=green][color=darkred]
>>> pattern.sub(':' , '3.')[/color][/color][/color]
'3:'
!----------------------------------------------------------------------!
any thoughts?
thanks,
aaron
Comment