| Store | Cart

regular expression

From: aaron <aste...@berkeley.edu>
Sat, 26 Mar 2005 02:07:15 GMT
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

Recent Messages in this Thread
aaron Mar 26, 2005 02:07 am
Bengt Richter Mar 26, 2005 04:30 am
Peter Hansen Mar 26, 2005 04:54 am
Bengt Richter Mar 26, 2005 07:41 am
Peter Hansen Mar 26, 2005 11:53 am
Paul McGuire Mar 27, 2005 07:05 am
Messages in this thread