Drew Nichols
Drew Nichols

Reputation: 755

URL regular expression help

Using this regular espression in my urls.py file, I get only the last letter in the add_or_remove param. The url hits my view, but I am getting only d for add and e for remove. What am I doing wrong?

r'(?P<add_or_remove>[add|remove])/'

Thanks

Upvotes: 1

Views: 168

Answers (3)

Jason McCreary
Jason McCreary

Reputation: 73031

I'll admit I'm not familiar with Python. But if your trying to capture add or remove, your syntax is wrong. That is to say you don't want the brackets as this is notation for a character set - matching any in the set.

r'(?P<add_or_remove>add|remove)/'

Upvotes: 0

Ivan Nevostruev
Ivan Nevostruev

Reputation: 28743

This happens because [...] matches any character from set. Just remove bracers (using bracers from (?P<>...) is usually enough):

r'(?P<add_or_remove>add|remove)/'

Upvotes: 1

bcoughlan
bcoughlan

Reputation: 26627

Change the square brackets to round () brackets. The way it is now matches any of the characters a d d | r e etc...

Upvotes: 1

Related Questions