On Sat, 26 Mar 2005 02:07:15 GMT, aaron <[EMAIL PROTECTED]> wrote: >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? > Brute force the exceptional case that happens at the start of the line?
>>> 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:' >>> pattern.sub(':', '.3') ':3' >>> pattern.sub(':', '3.') '3:' Seems like an asymmetry in re's handling of (?!\d) after the last char vs before first though. Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list