Match path but not os.path
<p>I want to match the string <code>path</code> but not the string <code>os.path</code>. How do I go about that ? Tried <a href="http://stackoverflow.com/questions/2953039/regular-expression-for-a-string-containing-one-word-but-not-another"><code>(?!(os\.path))\bpath\b</code></a> but I still get all <code>os.path</code>S</p>

<p>If you want to skip any <code>path</code> starting with <code>.</code> try</p>

<p><code>(?&lt;!\.)path</code></p>

<p>This will skip <code>sys.path</code> or <code>os.path</code> but will match <code>path</code>.</p>

<p>e.g. if test strings is <code>if path and not os.path.sep in path</code>,</p>

<p>match will be:</p>

<p>if <code>path</code> and not os.path.sep in <code>path</code></p>

<p>See demo at <a href="https://regex101.com/r/5WM8A1/1" rel="nofollow">regex101</a></p>

python regex python-2.7