Regular Expressions?
David M. Cooke
cookedm+news at physics.mcmaster.ca
Wed Feb 25 17:28:35 EST 2004
At some point, "Diez B. Roggisch" <deets_noospaam at web.de> wrote:
>> I want to use a regular expression like the following:
>>
>> "*[\s|\w]*" + x +"[\s|\w]*\" + y + "*[\s|\w]*\" + y + "*[\s|\w]*"
>>
>> where x and y and varaiblae names and their value changes everytime.
>> For some reason this gives me an Invalid Token error. Can I use
>> variables in a regular Expression?
>
> Sure. But what you wrote above is most probably syntactically wrong:
>
> "*[\s|\w]*" + x +"[\s|\w]*\" + y + "*[\s|\w]*\" + y + "*[\s|\w]*"
> ^^
> You escape the d'qoutes, so that not the variable is inserted but instead
> the text " + y + ". Then a [ starts, which denotes the beginning of a list
> - a thing not supported right here. The same problem arises before the
> second y.
>
> This works for me:
>
> "*[\s|\w]*" + x +"[\s|\w]*" + y + "*[\s|\w]*" + y + "*[\s|\w]*"
>
> However, I suggest you use the %-operator in such cases:
>
> "*[\s|\w]*%s[\s|\w]*%s*[\s|\w]*%s*[\s|\w]*" % (x, y, y)
I'd suggest using re.escape() to make sure there's no nasty surprises
inside of x and y:
"*[\s|\w]*%s[\s|\w]*%s*[\s|\w]*%s*[\s|\w]*" % (re.escape(x),
re.escape(y), re.escape(y))
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
More information about the Python-list
mailing list