Python Regular Expression Index Numbers
<p>Just looking for some confirmation on this, but it appears that the index/position numbers for regular expressions do not follow the same rules used in the rest of python.</p>

<p>Example:</p>

<pre><code>pattern=re.compile('&lt;HTML&gt;')
pattern.search("&lt;HTML&gt;")
</code></pre>

<p>output:</p>

<pre><code>&lt;_sre.SRE_Match object; span=(0, 6), match='&lt;HTML&gt;'&gt;
</code></pre>

<p>Why is "span=(0, 6)"?</p>

<p>In python, the string  <code>"&lt;HTML&gt;"</code> is only 6 characters in length and therefore would return an index error when attempting to do something like:</p>

<pre><code>"&lt;HTML&gt;"[6]
File "&lt;stdin&gt;", line 1, in &lt;module&gt;
IndexError: string index out of range
</code></pre>

<p>So I'm fairly certain the answer is that this span value for match objects is inherently different than index values for python data structures. While the span value for matched objects starts at 0 for the first character(like with all python data structures) the last character is always endpos-1.</p>

<p>If anyone can confirm my assumption and maybe explain why this difference exists I would greatly appreciate it.</p>

<p>Well a slice (span) in Python is open ended. So <code>"&lt;HTML&gt;and much more"[0:6]</code> actually returns <code>"&lt;HTML&gt;"</code>.</p>

python regex