在字符串中获取网址的最快方法

时间:2019-03-30 12:24:21

标签: python regex findall

我必须检查数千个字符串,我需要获取包含{initError-[cf][4]}_Invalid nodes(s): [3]

的完整URL。

到目前为止,我正在使用此方法:

instagram.com/p/

但是某些URL无法找到。

我想获取所有类似于以下内容的网址:

msg ='hello there http://instagram.com/p/BvluRHRhN16/' msg = re.findall( 'http[s]?://?[\w/\-?=%.]+instagram.com/p/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', msg) print(msg) https://instagram.com/p/BvluRHRhN16/ https://www.instagram.com/p/BvluRHRhN16/ http://instagram.com/p/BvluRHRhN16/ https://www.instagram.com/p/BvluRHRhN16/

如何以最快的方式获得此结果?

2 个答案:

答案 0 :(得分:1)

我假设输入的是包含URL的句子列表。希望这会有所帮助。

msg =['hello there http://google.com/p/BvluRHRhN16/ this is a test',
      'hello there https://www.instagram.com/p/BvluRHRhN16/',
      'hello there www.instagram.com/p/BvluRHRhN16/ this is a test',
      'hello there https://www.instagram.net/p/BvluRHRhN16/ this is a test'
     ]

for m in msg:
    ms = re.findall('(http.*instagram.+\/p.+|www.*instagram.+\/p.+)',m)
    print(ms)

编辑的正则表达式:

ms = re.findall('(http.*instagram\.com\/p.+\/|www.*instagram\.com\/p.+\/)',m)

答案 1 :(得分:1)

filtered = ([item for item in urls if "instagram.com/p/" in item])

print(filtered)

输出: ['the documentation','http://google.com/p/BvluRHRhN16/','www.instagram.com/p/BvluRHRhN16/','https://www.instagram.com/p/BvluRHRhN16/']

已修改:过滤网址的

            SET @DataXml.modify(' replace value of (/*/Plans/Plan[sql:variable("@PlanID")]/Details/IsSelected/text())[1] with sql:variable("IsSelectedValue")') 

输出: ['https://www.instagram.net/p/BvluRHRhN16/','www.instagram.com/p/BvluRHRhN16 /']