Wizard
Wizard

Reputation: 22113

What does P mean in `/(?P<topic_id>\d+)$`

This might be frivolous, I don’t know the origin of P in /(?P<topic_id>\d+)$",

Google searches aren’t helpful. Python official docs does not elaborate it on 6.2. re

    [ # page for adding a new new Entry
    url(r"^new_entry/(?P<topic_id>\d+)$", views.new_entry, name='new_entry'), ]

Does the ‘P’ mean ‘pattern’ which seems unnecessary to declare it.

Upvotes: 1

Views: 1570

Answers (1)

sshine
sshine

Reputation: 16135

(?P<name>...) is a named group:

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.

As such, it's equivalent to (...) but instead of referring to \1, you can refer to any of the following: (?P=name), \1, m.group('name'), or \g<name>, depending on the context.

Upvotes: 2

Related Questions