import re
regex = re.compile(r"O(?<=^.{0,8})", flags=re.MULTILINE)
test_str = ("C099900XABCD\n\n"
"O099900XABCD\n"
"CO99900XABCD\n"
"C0O9900XABCD\n"
"C09O900XABCD\n"
"C099O00XABCD\n"
"C0999O0XABCD\n"
"C09990OXABCD\n"
"C099900OABCD\n\n"
"C099900XOBCD\n"
"C099900XAOCD\n"
"C099900XABOD\n"
"C099900XABCO")
subst = "0"
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