What does this Django regular expression mean? `?P`
Asked Answered
O

5

92

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]+)/$
Outcry answered 3/11, 2011 at 0:20 Comment(0)
T
66

(?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

Thankyou answered 3/11, 2011 at 0:24 Comment(0)
L
94

In django, named capturing groups are passed to your view as keyword arguments.

Unnamed capturing groups (just a parenthesis) are passed to your view as arguments.

The ?P is a named capturing group, as opposed to an unnamed capturing group.

http://docs.python.org/library/re.html

(?P<name>...) Similar to regular parentheses, but the substring matched by the group is accessible within the rest of the regular expression via the symbolic group name name. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named. So the group named id in the example below can also be referenced as the numbered group 1.

Laszlo answered 3/11, 2011 at 0:32 Comment(0)
T
66

(?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

Thankyou answered 3/11, 2011 at 0:24 Comment(0)
C
27

(?P<category_slug>) creates a match group named category_slug.

The regex itself matches a string starting with category/ and then a mix of alphanumeric characters, the dash - and the underscore _, followed by a trailing slash.

Example URLs accepted by the regex:

  • category/foo/
  • category/foo_bar-baz/
  • category/12345/
  • category/q1e2_asdf/
Collis answered 3/11, 2011 at 0:22 Comment(9)
Does that mean whatever shows up there (abiding to the laws of [-\w] of course) is assigned to a variable category_slug?Outcry
I'm not familiar with django but the most likely behaviour is that the view function will receive a keyword argument named category_slutCollis
@cfarm54 -- The re module provides functions to match the expression. They return a Match object if text matches the regex. Then match.group('category_slug') returns the contents of the group ('foo', 'foo_bar-baz', etc.).Glooming
@ThiefMaster: Best typo everWigwam
@Collis +1 for the category_slutToronto
@Collis What were you thinking;)Zacek
@Collis your comment made my day.Blowhard
@Collis your comment is now immortalizedBiak
after reading the @Collis comment, I have recognized the weird slug in the example, and understood him :D But if this is a typo – nice one ))Mosaic
M
-2

New in version 3.6.

(?P<name>...) Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name name. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named.

copy paste from Python3Regex

Marchetti answered 20/5, 2018 at 10:40 Comment(0)
M
-2

In pattern matching, Use this pattern for passing string

(?P<username2>[-\w]+)

This for interger value

(?P<user_id>[0-9]+)
Methylamine answered 27/11, 2019 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.