Finding a pattern in python using re (reggae)
<p>I'm trying to build a regular expression:</p>

<pre><code>mystring = /some/path/with/%variable%/%inside%/%it%/
re.findall("[^\s%]+", allocine_spec[key])
</code></pre>

<p>And this return the following:</p>

<pre><code>['/some/path/', 'variable', '/', 'inside', '/', 'it']
</code></pre>

<p>But I would like only:</p>

<pre><code>['variable', 'inside', 'it']
</code></pre>

<p>Is that possible using only re?</p>

<p>You need a much simpler regex:</p>

<pre><code>%([^%]+)%
</code></pre>

<p>See the <a href="https://regex101.com/r/iP1gV6/1">regex demo</a></p>

<p>This will match a <code>%</code>, then capture 1+ chars other than <code>%</code> into Group 1 and then will match a trailing <code>%</code>. If your strings only contain word characters in between <code>%</code>, you may replace <code>[^%]+</code> with <code>\w+</code>.</p>

<p>Note that <code>re.findall</code> returns only the captured substrings if capturing groups are defined in the pattern.</p>

<p><a href="https://ideone.com/xDC4c0">Python demo</a>:</p>

<pre><code>import re
mystring = "/some/path/with/%variable%/%inside%/%it%/"
print(re.findall("%([^%]+)%", mystring))
# =&gt; ['variable', 'inside', 'it']
</code></pre>

python regex python-2.7