Python fails to compile a regex
<p>I'm trying to detect all <code>set</code> from a cmake file using a python regex, fo the file below:</p>

<pre><code># Library to include
set(LIB_TO_INCLUDE 
        a
        b
        c)

# comon code (inclusion in source code)
set(SHARED_TO_INCLUDE d e f)

# Library to include
set(THIRD_PARTY g h)
</code></pre>

<p>I'd like to retrieve:</p>

<pre><code>LIB_TO_INCLUDE a b c
SHARED_TO_INCLUDE d e f
THIRD_PARTY g h
</code></pre>

<p>I tested the regex <code>set\((?s:[^)])*?\)</code> (get all but <code>)</code> items following <code>set(</code>) using regex101.com (see <a href="https://regex101.com/r/aB5tX2/1" rel="nofollow">https://regex101.com/r/aB5tX2/1</a>), it apparently does what I want.</p>

<p>Now when I try to run <code>re.compile(r'set\((?s:[^)])*?\)')</code> from Python, I get the error:</p>

<pre><code>  File "private\python_scripts\convert.py", line 34, in create_sde_files
    pattern = re.compile(r'set\((?s:[^)])*?\)')   File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\re.py", line 223, in compile
    return _compile(pattern, flags)   File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\re.py", line 294, in _compile
    p = sre_compile.compile(pattern, flags)   File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\sre_compile.py", line 568, in compile
    p = sre_parse.parse(p, flags)   File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\sre_parse.py", line 760, in parse
    p = _parse_sub(source, pattern, 0)   File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\sre_parse.py", line 370, in _parse_sub
    itemsappend(_parse(source, state))   File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\sre_parse.py", line 721, in _parse
    raise error("unknown extension") sre_constants.error: unknown extension
</code></pre>

<p>Is this kind of regex not supported by Python?</p>

<p>This should do it: <code>set\(([^)]*?)\)</code></p>

<p>The "single line" modifier is passed as an argument when you compile the regex:</p>

<pre><code>&gt;&gt;&gt; t = """set(LIB_TO_INCLUDE 
...         a
...         b
...         c)"""
&gt;&gt;&gt; 
&gt;&gt;&gt; pattern = r'set\(([^)]*?)\)'
&gt;&gt;&gt; 
&gt;&gt;&gt; regex = re.compile(pattern, re.S)
&gt;&gt;&gt; 
&gt;&gt;&gt; result = regex.search(t).groups()[0]
&gt;&gt;&gt; result
'LIB_TO_INCLUDE \n        a\n        b\n        c'
</code></pre>

<p>You can then eliminate the extra spacing and new lines:</p>

<pre><code>&gt;&gt;&gt; ' '.join(x.strip() for x in result.split('\n'))
'LIB_TO_INCLUDE a b c'
</code></pre>

<p>Note than in your link, if you switch to "python" in the "Flavors" on the left you'll get the errors that your particular format was causing.</p>

<p><strong>EDIT:</strong> to get all (3) matches you need to use <code>&lt;regex&gt;.findall(...)</code> instead of <code>search</code>.</p>

<pre><code>&gt;&gt;&gt; tt = """# Library to include
... set(LIB_TO_INCLUDE 
...         a
...         b
...         c)
... 
... # comon code (inclusion in source code)
... set(SHARED_TO_INCLUDE d e f)
... 
... # Library to include
... set(THIRD_PARTY g h)"""
&gt;&gt;&gt; 

&gt;&gt;&gt; result = regex.findall(tt)
&gt;&gt;&gt; result
['LIB_TO_INCLUDE \n        a\n        b\n        c', 'SHARED_TO_INCLUDE d e f', 'THIRD_PARTY g h']
</code></pre>

python regex