Python正则表达式捕获各种url模式组

2024-03-28

我有包含这样的字符串的数据集,我想从中删除所有网址

http://google.com having trouble finding regex https://google.com for this case http // google com / test some gibberish https // google . com / test / test1 great http.//google.org

现在,我使用这个正则表达式模式来查找所有网址:

https?:?\s?\/\/\s?\S+

现在,理想情况下,它应该捕获所有 url,例如在本例中,

  • http://google.com

  • https://google.com

  • http // google com / test

  • https // google . com / test / test1

  • http.//google.org

但使用我的正则表达式模式,它仅捕获

  • http://google.com

  • https://google.com

  • http // google

  • https // google

Link to .


您可以使用

https?[:.]?\s?\/\/(?:\s*[^\/\s.]+)+(?:\s*\.\s*[^\/\s.]+)*(?:\s*\/\s*[^\/\s]+)*

See the 正则表达式演示 https://regex101.com/r/syMCD7/3.

Details

  • https? - http or https
  • [:.]?- 可选的: or .
  • \s?- 可选的空白 -\/\/ - //字符序列
  • (?:\s*[^\/\s.]+)+ - (to match all domain name parts till the last . before TLD) 1 or more occurrences of
    • \s*- 0个或多个空格
    • [^\/\s.]+- 1 个或多个字符以外的字符/, .和空白
  • (?:\s*\.\s*[^\/\s.]+)* - 0 or more sequences of
    • \s*\.\s*- 一个由 0+ 个空格包围的点
    • [^\/\s.]+- 1 个或多个字符以外的字符/, .和空白
  • (?:\s*\/\s*[^\/\s]+)* - 0 or more sequences of
    • \s*\/\s* - a /包含 0+ 个空格
    • [^\/\s]+- 1 个或多个字符以外的字符/和空白
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python正则表达式捕获各种url模式组 的相关文章

随机推荐