regular expression

Paul McGuire ptmcg at austin.rr.com
Sun Mar 27 02:05:33 EST 2005


Aaron -

Here's a pyparsing approach (requires latest 1.3 pyparsing version).
It may not be as terse or fast as your regexp, but it may be easier to
maintain.

By defining floatNum ahead of DOT in the scanner definition, you
specify the dot-containing expressions that you do *not* want to have
dots converted to colons.

-- Paul

===================
from pyparsing import Word,Literal,replaceWith, Combine, nums

DOT = Literal(".").setParseAction( replaceWith(":") )
floatNum = Combine( Word(nums) + "." + Word(nums) )

scanner = floatNum | DOT

testdata = "'375 mi. south of U.C.B is 3.4 degrees warmer."

print scanner.transformString( testdata )
===================
prints out:
'375 mi: south of U:C:B is 3.4 degrees warmer:




More information about the Python-list mailing list