regex match proc name without slash
<p>I have a list of proc names on Linux.  Some have slash, some don't. For example,</p>

<p><strong>kworker</strong>/23:1</p>
<strong>migration</strong>/39</p>
<strong>qmgr</strong></p>

<p>I need to extract just the proc name without the slash and the rest.  I tried a few different ways but still won't get it completely correct.  What's wrong with my regex?  Any help would be much appreciated.</p>

<pre><code>&gt;&gt;&gt; str='kworker/23:1'
&gt;&gt;&gt; match=re.search(r'^(.+)\/*',str)
&gt;&gt;&gt; match.group(1)
'kworker/23:1'
</code></pre>

<p>An alternative to regex is to <code>split</code> on <em>slash</em> and take the first item:</p>

<pre><code>&gt;&gt;&gt; s ='kworker/23:1'
&gt;&gt;&gt; s.split('/')[0]
'kworker'
</code></pre>

<p>This also works when the string does not contain a slash:</p>

<pre><code>&gt;&gt;&gt; s = 'qmgr'
&gt;&gt;&gt; s.split('/')[0]
'qmgr'
</code></pre>

<p>But if you're going to stick to <code>re</code>, I think <code>re.sub</code> is what you want, as you won't need to fetch the <em>matching group</em>:</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; s ='kworker/23:1'
&gt;&gt;&gt; re.sub(r'/.*$', '', s)
'kworker'
</code></pre>

<p>On a side note, assignig the name <code>str</code> shadows the in built string type, which you don't want.</p>

python regex