Search code examples
pythonregexdjangocapturing-group

What does this Django regular expression mean? `?P`


I have the following regular expression (regex) in my urls.py and I'd like to know what it means. Specifically the (?P<category_slug> portion of the regex.

r'^category/(?P<category_slug>[-\w]+)/$

Solution

  • (?P<name>regex) - Round brackets group the regex between them. They capture the text matched by the regex inside them that can be referenced by the name between the sharp brackets. The name may consist of letters and digits.

    Copy paste from: http://www.regular-expressions.info/refext.html