I'm trying to find a reliable regex pattern for parsing a PCI address from a listing in sysfs
.
For example:
s = "
# total 0
# drwxr-xr-x 7 root root 0 Mar 22 21:30 .
# drwxr-xr-x 121 root root 0 Mar 22 21:27 ..
# drwxr-xr-x 2 root root 0 Mar 22 21:27 0000:13:45.6:pcie001
# drwxr-xr-x 2 root root 0 Mar 22 21:30 0000:12:34.5
# drwxr-xr-x 2 root root 0 Mar 22 21:30 0000:12:34.6
# -r--r--r-- 1 root root 4096 Mar 22 21:29 aer_dev_correctable
"
pattern = r'SOME MAGIC'
list_of_addrs = re.findall(pattern, s, re.MULTILINE)
where I expect list_of_addrs = ['0000:13:45.6:pcie001', '0000:12:34.5', '0000:12:34.6']
The pattern I'm approximately trying to encode as a regular expression is:
# Starts with a set of 4 hex characters, [0-9a-fA-F]
# :
# Set of 2 hex characters
# :
# Set of 2 hex characters
# Set of 1 hex characters
# Until next whitespace