import re
regex = re.compile(r"\b(?:(port)|(dpi)|(fb_height)|(fb_width))=\"\K\d+")
test_str = ("bst.instance.red.port=\"5555\"\n"
"bst.instance.red.dpi=\"240\"\n"
"bst.instance.red.fb_height=\"900\"\n"
"bst.instance.red.fb_width=\"1600\"\n"
"bst.instance.red.first_boot=\"0\"\n"
"bst.instance.redb.port=\"2000\"\n"
"bst.instance.redb.dpi=\"120\"\n"
"bst.instance.redb.fb_height=\"300\"\n"
"bst.instance.redb.fb_width=\"800\"\n"
"bst.instance.redb.first_boot=\"0\"\n"
"bst.instance.redc.port=\"2000\"\n"
"bst.instance.redc.dpi=\"120\"\n"
"bst.instance.redc.fb_height=\"300\"\n"
"bst.instance.redc.fb_width=\"800\"\n"
"bst.instance.redc.first_boot=\"0\"")
subst = "${1:+111}${2:+222}${3:+333}${4:+444}"
result = regex.sub(subst, test_str)
if result:
print(result)
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