Help with RegExps

Alex alex at somewhere.round.here
Mon Sep 27 14:21:28 EDT 1999


> > >>> r=re.compile('(^[^<>]+|>[^>]+)(#.*)')
> > >>> print re.sub(r,f,s)
> 
> There are some problems with that pattern: # at beginning of line
> isn't matched, two or more #'s aren't matches correctly.

Sorry about that.  You are probably right that parsing is the proper way
to go.  Someone usually suggests that when a request for a complex
regular expression is posted in this newsgroup.

I pretty much just learnt of the function-as-substitution thing myself,
so I haven't figured out the best way to use it.  Your createFun looks
like it will do the job.  The only thing I can suggest that might help
is wrapping it in a class:

import re
class colourTag:
  def __init__(self,colorName):
    self.colorName=colorName
  def insert_tag(self,match):
    group=match.group
    return '%s<font color=%s>%s</font>'%( \
      group(1),self.colorName,group(2))
  def __call__(self,match):
    return self.insert_tag(match)

tag=colourTag('blue')
s='some text <a href=#someRef>ref</a> # this is a comment'
r=re.compile('(^[^<>]+|>[^>]+)(#.*)')
print re.sub(r,tag,s)
tag.colorName='red'
print re.sub(r,tag,s)

Alex.




More information about the Python-list mailing list