【问题标题】:How can I separate links with spaces using regex如何使用正则表达式分隔带空格的链接
【发布时间】:2020-08-29 09:23:19
【问题描述】:

我有一个表格,其中 2 个链接可能放置在 1 个单元格中;此外,它包含无法删除的空格,因为链接将无法正常工作

我试过这段代码

import re
text = 'https://unior-textile.ru/image/cache/WhatsApp Image 2020-04-16 at 14.02.35-900x900.png https://unior-textile.ru/image/cache/WhatsApp Image 2020-04-16 at 13.52.03-900x900.png'
urls = re.findall('http][s]?://(?:[a-zA-Z]|[ ]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+.png', text)
print("Urls: ",urls)

但是得到了这个输出

Urls:  []

如何将这些链接分别提取到数组/列表中?

【问题讨论】:

    标签: python regex hyperlink


    【解决方案1】:

    你可以使用split

    import re
    
    text = (
        'https://unior-textile.ru/image/cache/WhatsApp Image 2020-04-16 at 14.02.35-900x900.png '
        'https://unior-textile.ru/image/cache/WhatsApp Image 2020-04-16 at 13.52.03-900x900.png'
    )
    
    links = [
        "https://" + link.strip()
        for link in re.split("https?://", text)
        if link
    ]
    

    结果:

    ['https://unior-textile.ru/image/cache/WhatsApp Image 2020-04-16 at 14.02.35-900x900.png',
     'https://unior-textile.ru/image/cache/WhatsApp Image 2020-04-16 at 13.52.03-900x900.png']
    

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      相关资源
      最近更新 更多