Regular Expressions?

Skip Montanaro skip at pobox.com
Tue Feb 24 22:05:09 EST 2004


    soheil2> I want to use a regular expression like the following: 
    soheil2> "*[\s|\w]*" + x +"[\s|\w]*\" + y + "*[\s|\w]*\" + y + "*[\s|\w]*"

    soheil2> where x and y and varaiblae names and their value changes
    soheil2> everytime. For some reason this gives me an Invalid Token
    soheil2> error. Can I use variables in a regular Expression?

Yes, you can, however you have two string literals in which you are escaping
the trailing quotation mark.  Try this instead:

    "*[\s|\w]*" + x +"[\s|\w]*\\" + y + "*[\s|\w]*\\" + y + "*[\s|\w]*"

It's not clear just what you're after, but I think this will probably lead
you further on the path to enlightenment:

    r"*[\s|\w]*%(x)s[\s|\w]*\%(y)s*[\s|\w]*\%(y)s*[\s|\w]*" % locals()

where x and y are local variables.

Skip




More information about the Python-list mailing list