Regular expression to find URLs within a string

前端 未结 27 2125
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 14:18

Does anyone know of a regular expression I could use to find URLs within a string? I\'ve found a lot of regular expressions on Google for determining if an entire string is

27条回答
  •  盖世英雄少女心
    2020-11-22 14:50

    text = """The link of this question: https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string
    Also there are some urls: www.google.com, facebook.com, http://test.com/method?param=wasd
    The code below catches all urls in text and returns urls in list."""
    
    urls = re.findall('(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+', text)
    print(urls)
    

    Output:

    [
        'https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string', 
        'www.google.com', 
        'facebook.com',
        'http://test.com/method?param=wasd'
    ]
    

提交回复
热议问题