import re
regex = re.compile(r"(?<!href=\"|\">)(https?:\/\/[\w\-\.!~?&=+\*\'(),\/]+)((?!\<\/\a\>).)*", flags=re.MULTILINE)
test_str = ("Test,\n\n"
"Here\\'s an interesting in-house litigation position with JPMorgan Chase in New York I thought you might be interested inL\n\n"
"<a href=\"http://www.xxx.com/lcdetail.php?akey=e1725\">http://www.xxx.com/lcdetail.php?akey=e1725</a>\n\n"
"They are not using recruiters to fill this job, so if you are interested you can just apply directly. \n\n"
"http://www.xxx.com/lcdetail.php?akey=e1725\n\n"
"Cordially\n"
"--Harrison")
subst = "<a href=\"$1\">$1</a>"
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