import re
regex = re.compile(r"_(?:(?!(?:.*?\d{6}))|(?=[^\d]+\d{8}))", flags=re.MULTILINE)
test_str = ("Patate_17890505_TitreEnCamelCase.ext\n"
"EPFL_AlgebreLineaire\n"
"TaxonomieProprietesFonctionsComplexes.v0.ipe.20210302_005606.pdf\n"
"1_\n"
"12_\n"
"_1\n"
"_12\n"
"12345678_\n"
"_123456\n"
"12345678_12345\n"
"1234567_123456\n"
"1234567_12345\n"
"123456_12345\n"
"12345678_1234567\n"
"123456789_123456\n"
"123456789_1234567\n"
"_patate__truc__\n"
"___\n"
"foo_12345678\n"
"foo_12345678_123456_bar\n"
"12345678_123456\n"
"foo12345678_123456bar")
subst = "."
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