1

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.

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • 1
    This isn't really a Python question: see this doc on [named groups in regexes](http://www.regular-expressions.info/named.html). – Daniel Roseman May 11 '18 at 06:45
  • 6
    Possible duplicate of [Named regular expression group "(?Pregexp)": what does "P" stand for?](https://stackoverflow.com/questions/10059673/named-regular-expression-group-pgroup-nameregexp-what-does-p-stand-for) – Gino Mempin May 11 '18 at 06:46

1 Answers1

2

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

sshine
  • 15,635
  • 1
  • 41
  • 66