我有包含这样字符串的数据集,我想从中删除所有 url
http://google.com 在这种情况下找不到正则表达式 https://google.com http//google com/测试一些乱码 https//google 。 com/test/test1 很棒的 http.//google.org
现在,我正在使用这个正则表达式模式来查找所有 url:
https?:?\s?\/\/\s?\S+
现在,理想情况下,它应该捕获所有 url,例如在这种情况下,
http://google.com
https://google.com
http//google com/test
https//谷歌。 com/test/test1
http.//google.org
但是使用我的正则表达式模式,它只捕获
http://google.com
https://google.com
http//google
https//google
链接到 Regex .
最佳答案
你可以使用
https?[:.]?\s?\/\/(?:\s*[^\/\s.]+)+(?:\s*\.\s*[^\/\s.]+)*(?:\s*\/\s*[^\/\s]+)*
参见 regex demo .
详情
https?
-http
或https
[:.]?
- 可选的:
或.
\s?
- 一个可选的空格 -\/\/
-//
字符序列(?:\s*[^\/\s.]+)+
-(匹配所有域名部分,直到 TLD 之前的最后一个.
)1或多次出现\s*
- 0 个或多个空格[^\/\s.]+
- 除了/
、.
和空格之外的 1 个或多个字符
(?:\s*\.\s*[^\/\s.]+)*
- 0 个或多个序列\s*\.\s*
- 用 0+ 个空格包围的点[^\/\s.]+
- 除了/
、.
和空格之外的 1 个或多个字符
(?:\s*\/\s*[^\/\s]+)*
- 0 个或多个序列\s*\/\s*
- 包含 0+ 个空格的/
[^\/\s]+
- 除了/
和空格之外的 1 个或多个字符
关于Python正则表达式捕获各种url模式组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62307275/