I guess it's possible, but i still didn't find any solution. I'm looking for a way to use Django URL pattern matcher for another data processing

For example:
I have following pattern
and following regular string

so what i want is to extract parameters by pattern form regular string in a dict shape

  "my_parameter": "test",
  "my_second_parameter: "123"

Pseudo code:

import matcher from django_matcher_package

my_pattern = "MySpecific-(?P<my_parameter>)-(?P<my_second_parameter>)"
my_string = "MySpecific-test-123"

extracted_values = matcher.match(my_pattern, my_string)
  • 2
    Django simply uses regex... – Stargazer Jun 12 at 13:01
  • Yes i know, but there is something else. This pattern seems to be regex non friendly test/url/<int:count> but it works fine – dorintufar Jun 12 at 13:15
  • @dorintufar: yes that's the new way of doing them, that is completely distinct from the regex-based ones. – RemcoGerlich Jun 12 at 13:20

That's just Python's regular expression support:

>>> pattern="MySpecific-(?P<my_parameter>.+)-(?P<my_second_parameter>.+)"
>>> s="MySpecific-test-123"
>>> import re
>>> match = re.match(pattern, s)
>>> match.groupdict()
{'my_parameter': 'test', 'my_second_parameter': '123'}

Note I added '.+' to your pattern twice, otherwise there wouldn't be anything to match in those groups.

  • This way will work 100%, but it will take too long to explain to system operators that after each parameter there should be additional .+ characters xD – dorintufar Jun 12 at 13:17
  • @dorintufar: they aren't extra, you also need them in your Django URLs. The one you had wouldn't match on your string in Django either. – RemcoGerlich Jun 12 at 13:19
  • Yep, i see. Thank you – dorintufar Jun 12 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.