Я решил это самостоятельно. Но я думаю, что это может помочь многим людям. Поэтому я отвечаю на свой вопрос и оставляю его в открытом доступе
Спасибо Николас . Его 30-50% решение мне очень помогло для полного решения.
import re
regex = r"href=\"\/"
test_str = ("<html>\n"
" <head>\n"
" <title>Hello</title>\n"
" </head>\n"
" <body>\n"
" <p>this is a simple text in html file</p>\n"
" <a href=\"https://google.com\">Google</a>\n"
" <a href=\"/front-end/login/\">Login</a>\n"
" <a href=\"/something/work/\">Something</a>\n"
" </body>\n"
" </html>")
subst = "href=\"/more/"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
subst2 = "\\1hello/"
regex2 = r"(href=\"/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)"
result2 = re.sub(regex2, subst2, result, 0, re.MULTILINE)
if result2:
print (result2)
writtingtofile = open("solution.html","w")
writtingtofile.write(result2)
writtingtofile.close()
Выход:
![enter image description here](https://i.stack.imgur.com/iP4pE.jpg)