import re
regex = re.compile(r"image(\d\d)$", flags=re.MULTILINE)
test_str = ("image1\n"
"image2\n"
"image3\n"
"image4\n"
"image5\n"
"image6\n"
"image7\n"
"image8\n"
"image9\n"
"image10\n"
"image11\n"
"image12\n"
"image13\n"
"image14\n"
"image15\n"
"image100\n"
"image101\n"
"image102\n"
"image103\n"
"image104\n"
"image105")
subst = "image0$1"
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