Regular expression help

Roman Suzi rnd at onego.ru
Thu May 3 11:01:34 EDT 2001


On Thu, 3 May 2001, Daniel Klein wrote:

Not very "educative", but it seem to work.
You can change your definition of string.
Now its letters + digits + _

>Help! I've _never_ done a regular expression and I've got an emergency
>situation where I need to find all occurrences of
>
>string1 + '(' + string2 + '<' + string3 + '>)'

----------------------------------------------------------------------

import re
str_dfn = "\s*([a-zA-Z0-9_]+)\s*"   # string definition
myre = """%s\(%s<%s>\s*\)""" % (str_dfn,str_dfn,str_dfn)
# print myre
mycre = re.compile(myre)

def do_something(m):
   s1, s2, s3 = m.group(1,2,3)    # string1,2,3
   print "FOUND:", (s1, s2, s3)
   return """ %s(%s<%s>) """ % (s1, s2, s3)

text = """  lalalala
   lllllllllll      X ( Y  <   Z >   )
abc123(def456<ghi789>)       llllllllllll dfsdfsdfsdf
>
>abc123(def456<ghi789>)
>or
>X   (   Y   <  Z   >   )

"""

txt = mycre.sub(do_something, text)
# print txt

----------------------------------------------------------------------
>
>and there could be spaces betwixt any of the components.
>
>For example,
>
>abc123(def456<ghi789>)
>or
>X   (   Y   <  Z   >   )
>
>Any immediate help appreciated. In the mean time, I'm going to try to work it
>out for meself.


Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Thursday, May 03, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "A conclusion is simply the place where you got tired of thinking." _/





More information about the Python-list mailing list