Regex url
Corey Richardson
kb1pkl at aim.com
Sat Jan 15 21:27:17 EST 2011
On 01/15/2011 08:48 PM, Jean-Francois wrote:
> Hi,
>
> I try to match the following url with one regex
>
> /hello
> /hello/
> /hello/world
> /hello/world/
>
>
> world is a variable, I can put toto instead
>
> Thanks !
What was the regex you tried, and where did it fail? I'm no re guru, but
here's my go at it:
"(/hello/?(%s)?/?)" % var
Interpreter session:
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> var = "toto"
>>> pat = re.compile("(/hello/?(%s)?/?)" % var)
>>> pat.match("/hello/toto")
<_sre.SRE_Match object at 0x7f53baf25938>
>>> pat.match("/hello")
<_sre.SRE_Match object at 0x7f53baf25c68>
>>> pat.match("/hello/")
<_sre.SRE_Match object at 0x7f53baf25938>
>>> pat.match("/hello/toto/")
<_sre.SRE_Match object at 0x7f53baf25c68>
More information about the Python-list
mailing list