Regular expression to find URLs within a string

前端 未结 27 1991
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  旧时难觅i
    2020-11-22 14:41

    A probably too simplistic, but working method might be:

    [localhost|http|https|ftp|file]+://[\w\S(\.|:|/)]+
    

    I tested it on Python and as long as the string parsing contains a space before and after and none in the url (which I have never seen before) it should be fine.

    Here is an online ide demonstrating it

    However here are some benefits of using it:

    • It recognises file: and localhost as well as ip addresses
    • It will never match without them
    • It does not mind unusual characters such as # or - (see url of this post)

提交回复
热议问题