Regex find whole substring between parenthesis containing exact substring
<p>For example I have string:</p>

<pre><code>"one two  (78-45ack sack); now (87 back sack) follow dollow (59 uhhaaa)"
</code></pre>

<p>and I need only whole substring with parenthesis, containing word <code>"back"</code>, for this string it will be:</p>

<pre><code>"(87 back sack)"
</code></pre>

<p>I've tried:</p>

<pre><code>(\(.*?back.*?\))
</code></pre>

<p>but it's return <code>"(78-45ack sack); now (87 back sack)"</code></p>

<p>How should my regex look like? As I understood it's happening cause search going from begin of the string, in common - <strong>how to perform regex to <code>"search"</code> from the middle of string, from the word <code>"back"</code>?</strong></p>

<p>You can use this regex based on negated character class:</p>

<pre><code>\([^()]*?back[^()]*\)
</code></pre>

<ul>
<li><code>[^()]*</code> matches 0 or more of any character that is not <code>(</code> and <code>)</code>, thus making sure we don't match across the pair of <code>(...)</code>.</li>
</ul>

<p><a href="https://regex101.com/r/hX7yW2/3" rel="nofollow">RegEx Demo 1</a></p>

<hr>

<p>Alternatively, you can also use this regex based on negative regex:</p>

<pre><code>\((?:(?!back|[()]).)*?back[^)]*\)
</code></pre>

<p><a href="https://regex101.com/r/hX7yW2/4" rel="nofollow">RegEx Demo 2</a></p>

<ul>
<li><code>(?!back|[()])</code> is negative lookahead that asserts that current position doesn't have <code>back</code> or <code>(</code> or <code>)</code> ahead in the text.</li>
<li><code>(?:(?!back|\)).)*?</code> matches 0 or more of any character that doesn't have <code>back</code> or <code>(</code> or <code>)</code> ahead.</li>
<li><code>[^)]*</code> matches anything but <code>)</code>.</li>
</ul>

python regex string substring parentheses