Regex and returning two lists as a tuple
<p>I have this function, that I would like to see if can be done more pythonic.
The function explains it self what it is trying to achieve.</p>

<p>My concern is that I'm using two regex expressions for <code>content</code> and <code>expected</code> that gives room for error creeping in, best would be if these two variables could use the same regex.</p>

<p>input example:</p>

<pre><code>test_names = "tests[\"Status code: \" +responseCode.code] = responseCode.code === 200;\ntests[\"Schema validator GetChecksumReleaseEventForAll\"] = tv4.validate(data, schema);"

def custom_steps(self, test_names):
    """ Extracts unique tests from postman collection """
    content = re.findall(r'(?&lt;=tests\[")(.*)(?::|"\])', test_names)
    expected = re.findall(r'(?&lt;=\] = )(.*)(?::|;)', test_names)
    for i, er in enumerate(expected):
        if "===" in er:
            expected[i] = er[er.find('===')+4:]
        else:
            expected[i] = "true"

    return content, expected
</code></pre>

<p>You can match both groups at the same time:</p>

<pre><code>def custom_steps(self, test_names):
    regex = 'tests\["(.*)(?::|"\]).* = (.+)(?::|;)'
    for match in re.finditer(regex, test_names):
        content, expected = match.groups()
        if '===' in expected:
            expected = expected[expected.index('===') + 4:]
        else:
            expected = 'true'
        yield content, expected
</code></pre>

<p>This gives you a generator over pairs of <code>content</code>, <code>expected</code>:</p>

<pre><code>for c, e in custom_steps(None, test_names):
    print c, e
</code></pre>

<p>Output:</p>

<pre><code>Status code 200
Schema validator GetChecksumReleaseEventForAll true
</code></pre>

python regex python-2.7