1

enter image description hereI am trying to match the text: details/1234/

using a regex: r'^details/(?P<article_id>[0-9]+)/$'

Where article_id is passed to the expression as a parameter. This is not returning any matches on Pythex and (obviously) Django.

I also tried:

r'^details/(?P<details>\w+)/$' but this doesn't return anything either. I'm lost. Could someone help?

Eg. string: localhost:8080/details/1234/

nikjohn
  • 20,026
  • 14
  • 50
  • 86
  • 1
    Can you provide an example that contains "details/1234" that you are using against your regex. – idjaw Oct 03 '15 at 18:12
  • Updated question with example string – nikjohn Oct 03 '15 at 18:16
  • That pattern is correct and works fine for the URL 'localhost:8080/details/1234/' – Daniel Roseman Oct 03 '15 at 18:18
  • Your regex has end and beginning of line anchors. Your example does not have a `/` at the end. So there is no way for your regex to match your example. – Ron Rosenfeld Oct 03 '15 at 18:22
  • @DanielRoseman - Added a screenshot from Pythex. Doesn't seem to be working :( @RonRosenfeld What would you suggest I change? I removed the second trailing slash and tried `localhost:8080/details/1234` and also, tried `localhost:8080/details/1234/` with the given regex. Pythex didn't find a match in either case – nikjohn Oct 03 '15 at 18:27
  • I don't understand what Pythex has to do with anything. This is Django, and Django matches against the path: "/details/1234/". That works absolutely fine with the pattern you have. – Daniel Roseman Oct 03 '15 at 18:38
  • I was just using Pythex to validate the RegEx. You're right - the issue itself resides in my Django code. I have posted a Django version of the question here: http://stackoverflow.com/q/32916267/1409180 Could you see if you could help? – nikjohn Oct 03 '15 at 18:49

3 Answers3

0

Why don't you use urlparse to help you out more?

from urlparse import urlparse

d = urlparse('http://localhost:8080/details/1234')

output:

ParseResult(scheme='http', netloc='localhost:8080', path='/details/1234', params='', query='', fragment='')

Getting path:

>>> d.path
'/details/1234'
>>>

If you want to use your regex after you extract the path, you can then use your regex with a small modification:

I've added /? which will look for '/' or no '/'.

^/?details/(?P<article_id>[0-9]+)$

pythex extracted that as:

article_id 1234

idjaw
  • 25,487
  • 7
  • 64
  • 83
  • I've added a screenshot from Pythex - could you tell me if I'm doing anything wrong here? – nikjohn Oct 03 '15 at 18:26
  • Yes. Get rid of the '^'. The '^' means to start from the beginning of the string. I removed that from your regex and it worked. – idjaw Oct 03 '15 at 18:27
  • If you use urlparse and then apply your regex it will work. Actually, a small modification to your regex will help you too. I'll update my answer. – idjaw Oct 03 '15 at 18:34
  • This certainly seems to be working on Pythex. Not on Django though - I have posted a Django version of the question here: http://stackoverflow.com/q/32916267/1409180 If you are good with Django, could you see if you could help me there? – nikjohn Oct 03 '15 at 18:51
0

try this;

/details\/(?P[0-9]+)/$/

I removed the ^ symbol at the beginning. Because that symbol (called carat) tries to search the pattern "from the start of the line", and your pattern details/... doesn't start at the start of the line !

I also removed the r (Python’s raw string notation) and then I escaped the / character to be consistent :) Pyhex scr

mayo
  • 3,845
  • 1
  • 32
  • 42
  • This certainly seems to be working on Pythex. Not on Django though - I have posted a Django version of the question here: http://stackoverflow.com/q/32916267/1409180 If you are good with Django, could you see if you could help me there? – nikjohn Oct 03 '15 at 18:51
  • Mmm, I'm not good at Django but I'm looking your question :) Did you removed the '^'?, seems that can be the cause of the problem ! – mayo Oct 03 '15 at 18:56
0

Here you try to pass a "Django-style" url with a carat ^ at the beginning. Django knows that all your urls in urls.py are relative and should start with 127.0.0.1:8000 or localhost:8080 or whatever you set when started a runserver or in your settings.py. Pythex do not know this, so it tries to find any matches where absolute path starts with "/details/..." and, of course, fails. Hence, removing a carat from the beginning of your regex should do the trick.

However, as Daniel Roseman already mentioned, that should work fine in django.

chem1st
  • 1,624
  • 1
  • 16
  • 22