Extract values between square brackets ignoring single quotes
<p>I am trying the following:</p>

<pre><code>s = "Text text text [123] ['text']"
</code></pre>

<p>This is my function:</p>

<pre><code>def getFromSquareBrackets(s):
    m = re.findall(r"\[([A-Za-z0-9_']+)\]", s)
    return m
</code></pre>

<p>But I am obtaining:</p>

<pre><code>['123', "'text'"]
</code></pre>

<p>I want to obtain:</p>

<pre><code>['123', 'text'] 
</code></pre>

<p>How can I ignore the single quotes?</p>

<p>You can make <code>'</code> optional using <code>?</code> as</p>

<pre><code>&gt;&gt;&gt; re.findall(r"\['?([^'\]]+)'?\]", s)
['123', 'text']
</code></pre>

<hr>

<ul>
<li><p><code>\['?</code> Matches <code>[</code> or <code>['</code>.</p></li>
<li><p><code>([^'\]]+)</code> Matches anything other than <code>'</code> or <code>]</code> and captures them.</p></li>
<li><p><code>'?\]</code> Matches <code>]</code> or <code>']</code></p></li>
</ul>

python regex