我需要一个在 gVim 中使用的正则表达式,它将从 URL 列表中删除重复的域(gVim 可以在这里下载:http://www.vim.org/download.php
我在 .txt 文件中有超过 6,000,000 个 URL 的列表(在 gVim 中打开以进行编辑)。
URL 的格式如下:
http://www.example.com/some-url.php
http://example2.com/another_url.html
http://example3.com/
http://www.example4.com/anotherURL.htm
http://www.example.com/some-url2.htm
http://example.com/some-url3.html
http://www.example2.com/somethingelse.php
http://example5.com
换句话说,URL 没有特定的格式。有些有 WWW,有些没有,它们都有不同的格式。
我需要一个为 gVim 编写的正则表达式,它将从列表中删除所有重复的域(及其对应的 URL),留下它找到的第一个实例。
所以它会采用上面发布的示例列表,最终结果应该如下所示:
http://www.example.com/some-url.php
http://example2.com/another_url.html
http://example3.com/
http://www.example4.com/anotherURL.htm
http://example5.com
这里有两个不错的站点,它们很好地解释了如何在 gVim 中使用正则表达式:
http://www.softpanorama.org/Editors/Vimorama/vim_regular_expressions.shtml
最佳答案
如果你想使用正则表达式,你可以尝试调整以下内容:%s!\v%(^http://%(www\.)?(%([^./]+\.)+[^./]+)%(/.*)?$\_.{-})@<=^http://%(www\.)?\1%(/.*)?\n!!g
,但它在 60 亿个 url 上会非常慢,并且由于未知原因而不起作用。这是一个更好的方法:
:let g:gotDomains={}
:%g/^/let curDomain=matchstr(getline('.'), '\v^http://%(www\.)?\zs[^/]+') | if !has_key(g:gotDomains, curDomain) | let g:gotDomains[curDomain]=1 | else | delete _ | endif
它正在做以下事情:
-
let g:gotDomains={}
创建一个空字典,我们将在其中保存所有域 -
%g/^/{command}
执行{command}
在每一行 let curDomain=matchstr(...)
获取域名-
getline('.')
从当前行开始 -
\v
请允许我省略在正则表达式中写很多反斜杠(非常神奇) -
^
从字符串开始 -
\zs
从这里开始匹配(省略捕获\zs
之前的所有内容)
-
if !has_key(g:gotDomains, curDomain)
如果之前没有出现域。-
let g:gotDomains[curDomain]=1
然后将其添加到已知域列表中(这里不需要1
,我使用字典只是为了更快地访问)。 -
delete _
否则删除到黑洞寄存器的行(这意味着,不要将其内容保存在任何寄存器中)。
关于windows - gVim 中的正则表达式从列表中删除重复域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4002115/