Web Hosting Talk







View Full Version : Regular Expressions [Python]


krumms
02-19-2003, 05:15 AM
Hi everyone,

Having a few troubles with regular expressions in Python...

The code:


# Python, not PHP :D

class parser:
# ... other crap ...

def __evaltag( self, tagdata ):

tag_regexp = re.compile( r'\W+' )
out = tag_regexp.split( tagdata )

if out:
return str( out )
else:
return ''


The above code is from the begginnings of a Python driven template engine. Now, what I'm currently trying to do is use MatchObject.split() to split my special template tags into seperate components. For example, my current test is an if statement

{if a eq 'b'**

The string passed to __evaltag is the text between the two curly braces.

When put through parser.__evaltag, the result looks like this:

['if', 'a', 'eq', 'b', \'\']

Now, I'm aware that my regular expression from above (\W+) should split up the string with every non alpha-numeric character, so this is correct behaviour. However, I want it to keep the quotes around 'b' such that it looks like this:

['if', 'a', 'eq', '\\'b\\'']

I know this is a long winded message, but can anyone help?