import re
regex = re.compile(r"(https?:\/\/)?(www\.)?(\S+)(\.[^\s\/]+)(\/\S*)?", flags=re.MULTILINE)
test_str = ("https://www.google.com\n"
"http://coreyms.com\n"
"https://www.youtube.com/watch?v=sa-TUpSx1JA\n"
"https://www.nasa.gov\n"
"www.desmos.com\n"
"pastebin.com/s8fh3HAs\n"
"https://www.regex101.com/\n"
"www.translate.google.com/\n"
"https://roblox.com/home\n"
"chat.openai.com/?model=text-davinci-002-render-sha\n"
"https://www.amazon.com/\n"
"agar.io/#ffa")
subst = "$3$4"
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