python regex match number followed by string or nothing
<p>how to match a number followed immediately by either /tcp or /udp or nothing?</p>

<p>the following illustrates 3 scenarios: input --> result</p>

<ol>
<li>123/tcp_haha --> 123</li>
<li>123  --> 123</li>
<li>123abc/tcp --> no match</li>
</ol>

<p>i used <code>re.compile(r'(\d+)(?:\/[tcpud]{3})*')</code>but it also matched in case 3.</p>

<p>EDIT:
Guess it's really a follow up question: how to match digits either followed by /tcp or /udp or proceeded by tcp/ or /udp or just by itself?
so </p>

<pre><code> 1.    something else 123/tcp_haha --&gt; 123
 2.    123  --&gt; 123
 3.    123abc/tcp --&gt; no match
 4.    udp/123 something else --&gt; 123
 5.    tcp/123/tcp --&gt; 123
</code></pre>