Passing values into regex match function
Posted
by Tim
on Stack Overflow
See other posts from Stack Overflow
or by Tim
Published on 2010-06-08T16:19:41Z
Indexed on
2010/06/08
16:22 UTC
Read the original article
Hit count: 198
In python (it's a Django filter), I'm doing this:
lReturn = re.sub(r'\[usecase:([ \w]+)]', r'EXTEND WITH <a href="/usecase/%s/\1/">\1</a>' % pCurrentProjectName, lReturn)
I'd like to use a function instead of a string (so I can check that the usercase is a valid name), so it would change to this:
def _match_function(matchobj):
lMatch = matchobj.group(1)
return "EXTEND WITH <a href='/usecase/%s/%s/'>%s</a>" % (pCurrentProjectName, lMatch, lMatch)
lReturn = re.sub(r'\[usecase:([ \w]+)]', _match_function, lReturn)
How do I get pCurrentProjectName into the _match_function() function?
© Stack Overflow or respective owner