python regex to split list entry's name into list name and index
<p>How can I use regular expressions to parse <code>&lt;listname&gt;[#]</code> into <code>[listname, #]</code>?</p>

<p>Here's what I've tried:</p>

<pre><code>&gt; s = 'li[10]'
&gt; re.split(s,r'[%d]')
['[%d]']
&gt; re.findall(s,r'[%d]')
[]
&gt; s.split(r'[%d]')
['li[0]']
</code></pre>

<p>the desired output is <code>li</code> and <code>10</code></p>

<p>First, you should put your pattern as the first part of the <code>findall</code> command as <code>findall( pattern, string)</code>.  You pattern is only matching a single digit ie: <code>0</code> through <code>9</code>.  To match more than 1 digit you can use:</p>

<pre><code> re.findall(r'\[(\d+)\]', s)
</code></pre>

<p>This will return just the digits, and not the square brackets.  To get the stuff in from the brackets you can use:</p>

<pre><code>re.findall(r'(\w)\[(\d+)\]', s)
</code></pre>

python regex