[python-win32] (no subject)

Gary Smith gary.smith28 at comcast.net
Wed Jul 22 21:59:30 CEST 2009


Hi All,

I built a Python com server to solve regular expressions for use in a  VBA
application.  Here's the code:

 

import pythoncom

import re

import string

class reObj:

            _public_methods_ = ["re"]

            _reg_progid_ = "Python.reObj"

            _reg_clsid_ = pythoncom.CreateGuid()

            def __init__ (self):

                        pass

            def re(self, pattern, str):

                        result=''

                        matchGroup=re.search(pattern,str)

                        while matchGroup<>None:

                                    (start,end)=matchGroup.span()

                                    result = result + ", " + str[start:end]

                                    str=str.replace(str[start:end],'',1)

                                    matchGroup=re.search(pattern,str)

                        return result[2:]

            def listem(self, list):

                        for item in list:

                                    print item, r.re(p,item)

 

if __name__ == '__main__':

            import win32com.server.register

            win32com.server.register.UseCommandLine(reObj)

 

The last few lines register the com server, so all that is necessary is to
execute the code.

 

Calling from VBA is:

    Dim re As Object

Set reObj = CreateObject("Python.reObj")

Result = reObj(pattern, string)

_

I got the basic idea from something I found on the web.

Cheers, Gary

__________________________________

"Even for practical purposes theory generally turns out the most important
thing in the end."  Oliver Wendell Holmes.

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20090722/96ad4912/attachment-0001.htm>


More information about the python-win32 mailing list