1

My url is in the format of

/en/33/details

But the number 33 is dynamic and I have to check if the url has /en/ part and /details part and some number in between.

I tried checking through

if url == r'^/en/(?d[0-9]+)/details':

But i checked through the django url regex syntax but it doesn't work. May I know where I am going wrong

1
  • 1
    Remove the '?d' from your regex Commented Oct 20, 2016 at 15:09

1 Answer 1

1

There is no (?d)-like regex construct in Python.

You need to use r'/en/\d+/details' with re.match.

if re.match(r'/en/\d+/details')

The re.match method anchors the search at the start of the string, so this will only find a match if

  • /en/ - there is /en/ at the start of the string, followed with
  • \d+ - 1 or more digits,
  • /details - a literal string /details.

If you need to also anchor the pattern at the end, append $:

if re.match(r'/en/\d+/details$')  
                             ^
Sign up to request clarification or add additional context in comments.

1 Comment

@Yogi then perhaps you should accept the answer... :P.

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.