Match alternate url - django urls regex

I want a Django url with two alternatives, /module/in/

or/module/out/

I use

url(r'^(?P<status>\w+[in|out])/$', 
'by_status', 
name='module_by-status'),

      

But it is consistent with other patterns, for example /module/i/

, /module/n/

and /module/ou/

.

Any hint is appreciated :)

+2


source to share


2 answers


Try r'^(?P<status>in|out)/$'

You need to remove \w+

that matches one or more alphanumeric or underscore characters. The regex suggested in bstpierre's answer '^(?P<status>\w+(in|out))/$'

will match helloin

, good_byeout

etc.



When I originally wrote this answer in 2009, Django was unable to reverse the regexes that used the pipe character |

. However, this restriction has since been lifted. The limitation is mentioned in the 1.4 docs but not the 1.5 docs , so I am assuming it was changed in Django 1.5.

+8


source


You want (in | out) you are using [] to specify a character class containing the characters "i", "n", "|", "o", "u", "t".



+1


source







All Articles