Extract string from regex
<p>For the below python code, I am using regex to parse the string. However I am struggling to extract the string from the matched pattern.</p>

<pre><code>import re

rx = re.compile(
    r'^(?P&lt;interesting&gt;.+?)-(?P&lt;uid&gt;\b\w{8}-(?:\w{4}-){3}\w{12}\b)(?P&lt;junk&gt;.+)$',
    re.MULTILINE | re.VERBOSE)

test_str = u"00000 Gin-12-a19ea68e-64bf-4471-b4d1-44f6bd9c1708-62fa6ae2-599c-4ff1-8249-bf6411ce3be7-83930e63-2149-40f0-b6ff-0838596a9b89 Kin\n00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708 Kin\ntest123 test 12345678-1234-1234-1234-123456789012 junk afterwards\n"
tmp = re.findall(rx, test_str)
print(tmp[0])
</code></pre>

<p>I get the below output</p>

<pre><code>('00000 Gin-12', 'a19ea68e-64bf-4471-b4d1-44f6bd9c1708', '-62fa6ae2-599c-4ff1-8249-bf6411ce3be7-83930e63-2149-40f0-b6ff-0838596a9b89 Kin')
</code></pre>

<p>My expected ouput is</p>

<pre><code>00000 Gin-12
</code></pre>

<p>You have a named group in your regex so just use it:</p>

<pre><code>import re

rx = re.compile(r'^(?P&lt;interesting&gt;.+?)-(?P&lt;uid&gt;\b\w{8}-(?:\w{4}-){3}\w{12}\b)(?P&lt;junk&gt;.+)$', re.MULTILINE | re.VERBOSE)

test_str = u"00000 Gin-12-a19ea68e-64bf-4471-b4d1-44f6bd9c1708-62fa6ae2-599c-4ff1-8249-bf6411ce3be7-83930e63-2149-40f0-b6ff-0838596a9b89 Kin\n00000 Gin-a19ea68e-64bf-4471-b4d1-44f6bd9c1708 Kin\ntest123 test 12345678-1234-1234-1234-123456789012 junk afterwards\n"
tmp = re.match(rx, test_str)
print(tmp.groupdict()["interesting"])
</code></pre>

python regex