匹配邮箱和网站地址并添加锚点

下面程序可以批量为文件中邮箱地址和网站地址添加锚点,方便使用URL访问。


def main():

    s = 'This is my email: zhanheng@gmail.com'

    pattern = re.compile(r'\b(\w+@\w+\.(?:com|org|net))\b')

    m = re.findall(pattern, s)

    print(m)



    ns = re.sub(pattern, r'<a href="mailto:\1">\1</a>', s)

    print(ns)



    s = 'maybe some tech stuff at https://www.zhblog.net!'

    pattern = re.compile(r'\bhttps?://(?:[-a-z0-9]+\.)*[-a-z0-9]+\.(?:com|edu|net)(?<![.,?!])', re.I)

    result = re.findall(pattern, s)

    print(result)



    ns = re.sub(r'\b(https?://'

                r'(?:[-a-z0-9]+\.)*[-a-z0-9]+\.(?:com|edu|net))\b',

                r'<a href="\1">\1</a>', s, flags=re.X)

    print(ns)





if __name__ == '__main__':

    main()


结果:

['zhanheng@gmail.com']

This is my email: <a href="mailto:zhanheng@gmail.com">zhanheng@gmail.com</a>

['https://www.zhblog.net']

maybe some tech stuff at <a href="https://www.zhblog.net">https://www.zhblog.net</a>!


 

展开阅读全文