2021年6月20日日曜日

文字列からURLを抽出する

<nodejs>

var urlpattern2 = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/;

var tmp = urlpattern2.exec(str_with_url);



<Python>

def FindUrl(string):

    # findall() has been used 

    # with valid conditions for urls in string

    regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"

    url = re.findall(regex,string)

    #return [x[0] for x in url]

    return [x[0] for x in url][0]


0 件のコメント:

コメントを投稿