Example string:
x=0, y=0, width=1920, height=1080, width_mm=531, height_mm=299, name='\\\\\\\\.\\\\DISPLAY4', is_primary=True
I want to get every value behind the "=" sign.
With
print(re.findall(r"=(.*?),", "x=0, y=0, width=1920, height=1080, width_mm=531, height_mm=299, name='\\\\\\\\.\\\\DISPLAY4', is_primary=True"))
I get:
['0', '0', '1920', '1080', '531', '299', "'\\\\\\\\.\\\\DISPLAY4'"]
But I want the "True" from "is_primary" too
With
=(.*?)(,|$)
I can split the string in two groups and fetch the values from group1 with a for loop but
i think, there is a more beautiful way and i really want to see it
And is it maybe even possible to get the
"DISPLAY4"
out of:
"'\\\\\\\\.\\\\DISPLAY4'"
in the same expression?
>Solution :
You can use re.findall and then exclude matching , or = before and after the = sign, using a single capture group.
If the values themselves can not contain ' you could use also exclude matching it:
[^=\s,]+=[\\.']*([^=\s,']+)
import re
pattern = r"[^=\s,]+=[\\.']*([^=\s,']+)"
s = "x=0, y=0, width=1920, height=1080, width_mm=531, height_mm=299, name='\\\\\\\\\\\\\\\\.\\\\\\\\DISPLAY4', is_primary=True"
print(re.findall(pattern, s))
A bit more precise match with 2 capture groups:
[^=\s,]+=(?:'(?:\\+\.\\+)?([^\s,=']+)'|([^\s,=]+))
import re
pattern = r"[^=\s,]+=(?:'(?:\\+\.\\+)?([^\s,=']+)'|([^\s,=]+))"
s = "x=0, y=0, width=1920, height=1080, width_mm=531, height_mm=299, name='\\\\\\\\\\\\\\\\.\\\\\\\\DISPLAY4', is_primary=True"
res = [m.group(1) if m.group(1) else m.group(2) for _, m in enumerate(re.finditer(pattern, s), start=1)]
print(res)
Both will output:
['0', '0', '1920', '1080', '531', '299', 'DISPLAY4', 'True']