views:

49

answers:

2

I'm using the Tornado framework (Python). I have the sluggable URLs working. But I have 3 different entries in the URL dispatcher. I was wondering if someone could help me transform it into one line.

This is what I have:

(r"/post/([0-9]+)/[a-zA-Z0-9\-]+", SpotHandler),
(r"/post/([0-9]+)/", SpotHandler),
(r"/post/([0-9]+)", SpotHandler),

I want it so that the following URLs all go to the same place.

http://domain.com/post/14

http://domain.com/post/14/

http://domain.com/post/14/any-text-it-doesnt-matter-what-it-is

+1  A: 

(r"/post/([0-9]+)/?[a-zA-Z_]*", SpotHandler),

"?" means previous thing can be there but need not be. "*" means zero or more

mawimawi
this will also catch urls like http://domain.com/post/14something
zed_0xff
Great, thanks much!
TylerW
@TylerW @mawimawi @zed_0xff what about this URL: `http://domain.com/post/14something` or `http://domain.com/post/14/something/` ~~ @TylerW what about if it's supposed to be `http://domain.com/post/14/something` and they goto `http://domain.com/post/14/otherthing`? I'm guessing that's not a business domain problem here...
drachenstern
Correct. My site doesn't use the end sting at all. Just the number after /post/. So /post/14/string1 and /post/14/string2 can go to the same thing.
TylerW
Keep in mind that the path can legally contain unicode, which your regex won't match, if that matters to you.
Nick Bastin
+2  A: 

r"/post/([0-9]+)(?:/[a-zA-Z_-]+|/)?"

zed_0xff