1

I have the following two regex patterns.

 url(r"^list/(?P<email>[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})?/?", MyFunction_ListAPIView.as_view()),
 url(r"^list/(?P<id>[\d+])/$", OtherFunction_ListAPIView.as_view()),

I wanted to have two separate functions for email and for id. If an email is passed MyFunction should be called however if a decimal value is passed then OtherFunction should be called.

I just passed in a decimal value like so - Here 11 is a decimal value and not regex. Yet it is still calling the same function. Any suggestions on what I might be doing wrong ?

http://127.0.0.1:8000/api/job/list/11/ 
M.javid
  • 6,387
  • 3
  • 41
  • 56
MistyD
  • 16,373
  • 40
  • 138
  • 240
  • Does it work if you remove second to last question mark in the first pattern? – Nathan Mills Sep 17 '18 at 06:26
  • Let me try that – MistyD Sep 17 '18 at 06:27
  • No it doesnt however my second one doesnt work either. I think thats because it is missing a + at the end.Also the if i remove the second to the last question mark in the first one then not specifying anything fails as well. I want it to work even if there is no email address- So for empty or a valid email it should work – MistyD Sep 17 '18 at 06:28
  • Also on that second regex the plus should probably be after the square brackets unless you are trying to match a literal plus sign. – Nathan Mills Sep 17 '18 at 06:32
  • the `.` in the first half of the email regex is a wild card and not a dot but that won't solve the issue – Sayse Sep 17 '18 at 06:33
  • What about adding a $ sign to end of email regex? – Nathan Mills Sep 17 '18 at 06:57

1 Answers1

2

The ?/? at the end of the first regex makes the email optional. I don't know what you have in the urls list, but I suggest you try your regex here https://regex101.com/ so you can debug easily any url.

Karim N Gorjux
  • 2,880
  • 22
  • 29
  • 1
    i know it makes it optional however why does an 11 get passed through when i explicitly mentioned that it needs to be an email address ? – MistyD Sep 17 '18 at 06:36
  • @MistyD - Its because the whole end of the line of the regex is optional so all it needs to match is `list/` which it will match – Sayse Sep 17 '18 at 06:38