Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match a negative number in Django URL dispatcher?

Tags:

regex

django

I have absolutely no idea how Regex works. I'm using this bit of Regex to match arguments for the URL dispatcher:

r'^/(?P<c>\d+)/(?P<b>\d+)/(?P<g>\w+)'

The issue is that the second value b will not match if it's a negative. How can I change the above regex so it matches negative numbers as well as positive ones for the second value?

like image 831
Kristina Brooks Avatar asked Dec 06 '22 19:12

Kristina Brooks


1 Answers

r'^/(?P<c>\d+)/(?P<b>-?\d+)/(?P<g>\w+)'

I've added the -?, which means: match one or zero - characters.

like image 111
Drekembe Avatar answered Mar 15 '23 09:03

Drekembe