python: regex - catch variable number of groups
<p>I have a string that looks like:</p>

<pre><code>TABLE_ENTRY.0[hex_number]= &lt;FIELD_1=hex_number, FIELD_2=hex_number..FIELD_X=hex&gt;
TABLE_ENTRY.1[hex_number]= &lt;FIELD_1=hex_number, FIELD_2=hex_number..FIELD_Y=hex&gt;
</code></pre>

<p>number of fields is unknown and varies from entry to entry, I want to capture
each entry separately with all of its fields and their values.</p>

<p>I came up with:</p>

<pre><code>([A-Z_0-9\.]+\[0x[0-9]+\]=)(0x[0-9]+|0):\s+&lt;(([A-Z_0-9]+)=(0x[0-9]+|0))
</code></pre>

<p>which matches the table entry and the first field, but I dont know how to account for variable number of fields.</p>

<p>for input:</p>

<pre><code>ENTRY_0[0x130]=0: &lt;FIELD_0=0, FIELD_1=0x140... FIELD_2=0xff3&gt;
</code></pre>

<p>output should be:</p>

<pre><code>ENTRY 0:
        FIELD_0=0
        FIELD_1=0x140
        FIELD_2=ff3
ENTRY 1:
        ...
</code></pre>

<p>Use a capture group for match unfit lengths:</p>

<pre><code>([A-Z_0-9\.]+\[0x[0-9]+\]=)\s+&lt;(([A-Z_0-9]+)=(0x[0-9]+|0),\s?)*([A-Z_0-9]+)=(0x[0-9]+|0)
</code></pre>

<p>The following part matches every number of fields with trailing comma and whitespace</p>

<pre><code>(([A-Z_0-9]+)=(0x[0-9]+|0),\s?)*
</code></pre>

<p>And <code>([A-Z_0-9]+)=(0x[0-9]+|0)</code> will match the latest field.</p>

<p>Demo: <a href="https://regex101.com/r/gP3oO6/1" rel="nofollow">https://regex101.com/r/gP3oO6/1</a></p>

<p>Note: If you don't want some groups you better to use non-capturing groups by adding <code>?:</code> at the leading of capture groups.(<code>(?: ...)</code>), and note that <code>(0x[0-9]+|0):\s+</code> as extra in your regex (based on your input pattern)</p>

python regex