title: Matching three underscore strings at the start of end of non-whitespace text chunks depending on immediate context
body: <p>In all cases listed below, I want to match only <code>___</code> (three underscores).</p>
  <p>To make it clear, I am trying to match 3 underscores either not immediately preceded or not immediately followed with an underscore character and:</p>
  <ul>
  <li>if a character other than whitespace and <code>_</code> precedes the <code>___</code>, match last 3 (<em>test3_</em><strong>___</strong>)</li>
  <li>if a character other than whitespace and <code>_</code> follows the <code>___</code>, match first 3 (<strong>___</strong><em>____test5</em>).</li>
  </ul>
  <p>See <a href="https://regex101.com/r/2cHd9m/1" rel="nofollow noreferrer">my current attempt</a>:</p>
  <pre><code>((?&lt;=_)___|___(?=_)|___)
  </code></pre>
  <p>Tested with:</p>
  <pre><code>test1___(test2 ___)
     test3____ ___
    ___test4 + ____test5
  </code></pre>
  <p>All cases are matching correctly except for &quot;test3&quot;: it matches first 3 underscores instead of the last 3.</p>