replace URLs in text with links to URLs

后端 未结 5 1923
逝去的感伤
逝去的感伤 2020-12-16 21:40

Using Python I want to replace all URLs in a body of text with links to those URLs, like what Gmail does. Can this be done in a one liner regular expression?

Edit: b

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 22:02

    I hunted around a lot, tried these solutions and was not happy with their readability or features, so I rolled the following:

    _urlfinderregex = re.compile(r'http([^\.\s]+\.[^\.\s]*)+[^\.\s]{2,}')
    
    def linkify(text, maxlinklength):
        def replacewithlink(matchobj):
            url = matchobj.group(0)
            text = unicode(url)
            if text.startswith('http://'):
                text = text.replace('http://', '', 1)
            elif text.startswith('https://'):
                text = text.replace('https://', '', 1)
    
            if text.startswith('www.'):
                text = text.replace('www.', '', 1)
    
            if len(text) > maxlinklength:
                halflength = maxlinklength / 2
                text = text[0:halflength] + '...' + text[len(text) - halflength:]
    
            return '' + text + ''
    
        if text != None and text != '':
            return _urlfinderregex.sub(replacewithlink, text)
        else:
            return ''
    

    You'll need to get a link out image, but that's pretty easy. This is specifically for user submitted text like comments which I assume is usually what people are dealing with.

提交回复
热议问题