I have the follwing Python code
import re
regex = r"SRC=(?P<src_ip>[\d\.]*).*?PROTO=(?P<proto>\w*).*?(SPT=(?P<src_port>\d*).*?DPT=(?P<dst_port>\d*)|TYPE=(?P<icmp_type>\d*).*?CODE=(?P<icmp_code>\d*))"
input = "<4>Nov 25 12:00:13 ubuntu kernel: [ 574.036758] iptableslogIN=enp0s3 OUT= MAC=44:85:00:ec:eb:2b:00:21:55:ef:8c:7f:08:00 SRC=10.24.136.232 DST=10.23.85.76 LEN=44 TOS=0x00 PREC=0x00 TTL=52 ID=5250 PROTO=TCP SPT=56325 DPT=55 WINDOW=1024 RES=0x00 SYN URGP=0"
m = re.match(regex, input)
print(m.groupdict())
which fails with a
Traceback (most recent call last):
File "C:/scratches/scratch.py", line 6, in <module>
print(m.groupdict())
AttributeError: 'NoneType' object has no attribute 'groupdict'
because input
was not matched.
The exact same regex and input is successful on regex101. Why?