Regex interpretaion in Django tutorial
<p>despite being familiar with the basics of regex, the tutorial for <a href="https://docs.djangoproject.com/en/1.10/intro/tutorial03/#writing-more-views" rel="nofollow">Django 1.10</a> does not go into much detail about how some regex generates dynamic links. 
I am looking at this specific snippet under the polls/urls.py: </p>

<pre><code>url(r'^(?P&lt;question_id&gt;[0-9]+)/$', views.detail, name='detail'),
</code></pre>

<p>Could someone go into detail about how that regex is interpreted? The quantifiers at the start of the capture group, <code>(?P&lt;question_id&gt;</code> do not make sense to me. Specifically, how does Django know that <code>&lt;question_id&gt;</code> is a foreign key?</p>

<p>The <code>(?P&lt;question_id&gt;.*)</code> Says everything captured by the regex inside the parenthesis will be in a named group called question_id.  It can be directly addressed.  So the regex doesn't know it is a foreign key or anything of the sort, just that there is a group named <code>question_id</code>.   The parenthesis isn't really matched in the incoming string.</p>

<p>The <code>[0-9]+</code> matches and numeric string 1 or more digits long.  </p>

<p><code>^</code> is the start of the string.  <code>$</code> is the end of the string.  <code>^, $, (?P&lt;question_id&gt;, and )</code> are somewhat meta and aren't impacted by the string so much as the string's position and how the regex extracted groups will be referenced.  </p>

<p>The captured group is passed to the view (detail in this case) as a keyword argument and it's up to the view to make use of it in a meaningful way. </p>

python regex django