dragon_cat dragon_cat - 15 days ago 5
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
re.compile(r'(\d+)(?:\/[tcpud]{3})*')
but it also matched in case 3.

Answer

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

r'^\d+(?:/tcp|/udp)?$'

?: 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:

r'^\d+(?:/tcp.*|/udp)?$'

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

Comments