Using regex, extract quoted strings that may contain nested quotes
<p>I have the following string:</p>

<pre class="lang-none prettyprint-override"><code>'Well, I've tried to say "How Doth the Little Busy Bee," but it all came different!' Alice replied in a very melancholy voice. She continued, 'I'll try again.'
</code></pre>

<p>Now, I wish to extract the following quotes:</p>

<pre class="lang-none prettyprint-override"><code>1. Well, I've tried to say "How Doth the Little Busy Bee," but it all came different!
2. How Doth the Little Busy Bee,
3. I'll try again.
</code></pre>

<p>I tried the following code but I'm not getting what I want. The <code>[^\1]*</code> is not working as expected. Or is the problem elsewhere?</p>

<pre><code>import re

s = "'Well, I've tried to say \"How Doth the Little Busy Bee,\" but it all came different!' Alice replied in a very melancholy voice. She continued, 'I'll try again.'"

for i, m in enumerate(re.finditer(r'([\'"])(?!(?:ve|m|re|s|t|d|ll))(?=([^\1]*)\1)', s)):
    print("\nGroup {:d}: ".format(i+1))
    for g in m.groups():
        print('  '+g)
</code></pre>

<p>If you <em>really</em> need to return all the results from a single regular expression applied only once, it will be necessary to use lookahead (<code>(?=findme)</code>) so the finding position goes back to the start after each match - see <a href="http://stackoverflow.com/questions/869809/combine-regexp#870506">this answer</a> for a more detailed explanation.</p>

<p>To prevent false matches, some clauses are also needed regarding the quotes that add complexity, e.g. the apostrophe in <code>I've</code> shouldn't count as an opening or closing quote. There's no single clear-cut way of doing this but the rules I've gone for are:</p>

<ol>
<li>An opening quote must not be immediately preceeded by a word character (e.g. letter). So for example, <code>A"</code> would not count as an opening quote but <code>,"</code> would count.</li>
<li>A closing quote must not be immediately followed by a word character (e.g. letter). So for example, <code>'B</code> would not count as a closing quote but <code>'.</code> would count.</li>
</ol>

<p>Applying the above rules leads to the following regular expression:</p>

<pre><code>(?=(?:(?&lt;!\w)'(\w.*?)'(?!\w)|"(\w.*?)"(?!\w)))
</code></pre>

<p><img src="http://i.stack.imgur.com/vJP88.png" alt="Regular expression visualization"></p>

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

<p>A good quick sanity check test on any possible candidate regular expression is to reverse the quotes. This has been done in the demo here: <a href="https://regex101.com/r/vX4cL9/1" rel="nofollow">https://regex101.com/r/vX4cL9/1</a></p>

python regex