dragon_cat dragon_cat - 1 year ago 97
Python Question

python regex match number followed by string or nothing

how to match a number followed immediately by either /tcp or /udp or nothing?

the following illustrates 3 scenario: input --> result

  1. 123/tcp_haha --> 123

  2. 123 --> 123

  3. 123abc/tcp --> no match

i used
but it also matched in case 3.

Answer Source

Character class will match any combinations of its included characters. You need to use a logical OR instead.


?: is a non-capture group notation and ? will make your non-capture group optional (for non-suffix cases).

If you want to capture the string if something followed the /tcp you can use following regex:


Demo: https://regex101.com/r/oUm0e9/1

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download