import re
regex = re.compile(r"^(.*\.)(\d+)\\(\n\1\d+\\)+$", flags=re.MULTILINE)
test_str = ("D:\\folder1\\folder2\\folder3\\Artifact\\Artifact-1.1\\\n"
"D:\\folder1\\folder2\\folder3\\Artifact\\Artifact-1.2\\\n"
"D:\\folder1\\folder2\\folder3\\Artifact\\Artifact-1.3\\\n"
"D:\\folder1\\folder2\\folder3\\Different_Artifact\\Different_Artifact-17.1\\\n"
"D:\\folder1\\folder2\\folder3\\Different_Artifact\\Different_Artifact-17.4\\\n"
"D:\\folder1\\folder2\\folder3\\Different_Content\\Different_Content-2.0\\\n"
"D:\\folder1\\folder2\\folder3\\folder4\\Different_Content\\Different_Content-2.0\\\n"
"D:\\folder1\\folder2\\folder3\\folder4\\Different_Content\\Different_Content-2.2\\")
subst = "$1$2\\\\"
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