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><strong>EDIT</strong></p>

<p>I modified my regex, it match properly even more complicated cases:</p>

<pre><code>(?=(?&lt;!\w|[!?.])('|\")(?!\s)(?P&lt;content&gt;(?:.(?!(?&lt;=(?=\1).)(?!\w)))*)\1(?!\w))
</code></pre>

<p><a href="https://regex101.com/r/Q0t3Rp/1" rel="nofollow">DEMO</a></p>

<p>It is now even more complicated, the main improvement is not matching directly after some of punctuation character (<code>[!?.]</code>) and better quote case separation. Verified on diversified examples.</p>

<p>The sentence will be in <code>content</code> captured group. Of course it has some restrictions, releted to usage of whitespaces, etc. But it should work with most of proper formatted sentences - or at least it work with examples.</p>

<ul>
<li><code>(?=(?&lt;!\w|[!?.])('|\")(?!\s)</code> - match the <code>'</code> or <code>"</code> not preceded by word or punctuation character (<code>(?&lt;!\w|[!?.])</code>) or not fallowed by whitespace(<code>(?!\s)</code>), the <code>'</code> or <code>"</code> part is captured in group 1 to further use,</li>
<li><code>(?P&lt;content&gt;(?:.(?!(?&lt;=(?=\1).)(?!\w)))*)\1(?!\w))</code> - match sentence, followed by
same char (<code>'</code> or <code>"</code> captured in group 1) as it was started, ignore other quotes</li>
</ul>

<p>It doesn't match whole sentence directly, but with capturing group nested in lookaround construct, so with global match modifier it will match also sentences inside sentences - because it directly match only the place before sentence starts.</p>

<p><strong>About your regex:</strong></p>

<p>I suppose, that by <code>[^\1]*</code> you meant any char but not one captured in group 1, but character class doesn't work this way, because it treats <code>\1</code> as an char in octal notation (which I think is some kind of whitespace) not a reference to capturing group. Take a look on <a href="https://regex101.com/r/eN7jU3/1" rel="nofollow">this example</a> - read explanation. Also compare matching of <a href="https://regex101.com/r/qF5pI5/1" rel="nofollow">THIS</a> and <a href="https://regex101.com/r/jA2dZ7/1" rel="nofollow">THIS</a> regex. </p>

<p>To achieve what you want, you should use lookaround, something like this: <a href="https://regex101.com/r/cH4fG8/1" rel="nofollow"><code>(')((?:.(?!\1))*.)</code></a> - capture the opening char, then match every char which is not followed by captured opening char, then capture one more char, which is directly before captured char - and you have whole content between chars you excluded.</p>

python regex