import re
regex = re.compile(r"(?<=sda1)\s+(?|(\d+.?\d+)[G,M.L,%]?)\s+(?|(\d+.?\d+)[G,M.L,%]?)\s+(?|(\d+.?\d+)[G,M.L,%]?)\s+(\d+)%", flags=re.MULTILINE)
test_str = ("Filesystem Size Used Avail Use% Mounted on\n"
"udev 3.8G 0 3.8G 0% /dev\n"
"tmpfs 806M 5.3M 800M 1% /run\n"
"/dev/mmcblk0p2 235G 59G 164G 27% /\n"
"tmpfs 4.0G 0 4.0G 0% /dev/shm\n"
"tmpfs 5.0M 48K 5.0M 1% /run/lock\n"
"/dev/mmcblk0p1 510M 63M 448M 13% /boot/firmware\n"
"/dev/sda1 137.8G 299.88G 127G 3% /media/usbplatte\n"
"tmpfs 806M 0 806M 0% /run/user/1000\n\n"
"/dev/sda1 139752 2662 129920 3% /media/usbplatte\n")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html