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

前端 未结 5 615
长情又很酷
长情又很酷 2021-01-30 00:46

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

5条回答
  •  自闭症患者
    2021-01-30 01:28

    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...) 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.

提交回复
热议问题