1

When I try runserver, there is an error at this regular expression:

url(r'^articles/get/(?<article_id>)\d+/$', views.article)

Can you please explain - where I was wrong?

1
  • 1
    ^articles/get/(?P<article_id>\d+)/$ - The P is missing. Commented May 2, 2016 at 7:03

1 Answer 1

2

You must be looking for

^articles/get/(?P<article_id>\d+)/$
                ^            ^^^^   

See the regex demo

The first issue is that you failed to use a named capture group correctly, and the second issue is that you did not capture anything by setting the closing ) right after the group name, while you want to capture 1+ digits with the \d+ into the article_id group.

Also, some reference on the named groups can be found here:

(?P<name>...)
Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name name.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, for testing Python regex, regex101.com is good, but there are still bugs. For the current expression, it will work as shown. Make sure you actually test the pattern in the real environment if you are using more sophisticated patterns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.