Regular Expression - old regex module vs. re module

Steve stever at cruzio.com
Thu Jun 29 14:53:49 EDT 2006


Hi All,

I'm having a tough time converting the following regex.compile patterns
into the new re.compile format.  There is also a differences in the
regsub.sub() vs. re.sub()

Could anyone lend a hand?


import regsub
import regex

import re    # << need conversion to this module

....

    """Convert perl style format symbology to printf tokens.

    Take a string and substitute computed printf tokens for perl style
    format symbology.

    For example:

        ###.##    yields %6.2f
        ########  yields %8d
        <<<<<     yields %-5s
    """


exponentPattern = regex.compile('\(^\|[^\\#]\)\(#+\.#+\*\*\*\*\)')
floatPattern = regex.compile('\(^\|[^\\#]\)\(#+\.#+\)')
integerPattern = regex.compile('\(^\|[^\\#]\)\(##+\)')
leftJustifiedStringPattern = regex.compile('\(^\|[^\\<]\)\(<<+\)')
rightJustifiedStringPattern = regex.compile('\(^\|[^\\>]\)\(>>+\)')



    while 1:                        # process all integer fields
        print("Testing Integer")
        if integerPattern.search(s) < 0: break
        print("Integer Match : ", integerPattern.search(s).span() )
#        i1 , i2 = integerPattern.regs[2]
        i1 , i2 = integerPattern.search(s).span()
        width_total = i2 - i1
        f = '%'+`width_total`+'d'
#        s = regsub.sub(integerPattern, '\\1'+f, s)
        s = integerPattern.sub(f, s)



Thanks in advance!

Steve




More information about the Python-list mailing list