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>The problem with the regex is, that the greedy <code>.+</code> is going until the end, because everything after it is optional, meaning it is kept as short as possible (essentially empty). To fix this replace the <code>.</code> with anything but a <code>/</code>.</p>

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

<p>works. You can test this regex <a href="https://regex101.com/r/Z8Cixr/1" rel="nofollow">here</a>. In case it is new to you, <code>[^\/]</code> matches anything, but a slash., as the <code>^</code> in the beginning inverts which characters are matched.</p>

<p>Alternatively, you can also use <code>split</code> as suggested by Moses Koledoye. <code>split</code> is often better for simple string manipulation, while regex enables you to perform very complex tasks with rather little code.</p>

python regex