通过Regex修剪网址,但不修剪root

时间:2013-06-28 12:01:54

标签: regex url dns trim

我有很多网址,大约100K。

它看起来像这样:

blog.example.com/ilovecats/2011/02/10/the-bling-ring/
blog.example.com/fas24
blog.example.com/morg
blog.example.com/whistlermoar/
blog.example.com/punny/
blog.example.com/punny/2012/10/
blog.example.com/punny/2012/10/01/my-mom-is-alien/
blog.example.com/anniesblog/2012/10/12/i-lost-my-iphone
blog.example.com/anniesblog/2012/10/page/3/
blog.example.com/anniesblog/2012/10/page/4
blog.example.com/anniesblog/2012/10/page/5
blog.example.com/alfva/
blog.example.com/dudewheresmycar/
blog.example.com/mynameisbilly/
blog.example.com/mynameisbilly/page/23/
blog.example.com/anotherflower/category/axel/
blog.example.com/naxramas/
blog.example.com/angeleoooo/
blog.example.com/angeleoooo/2011/01/01/
blog.example.com/angeleoooo/2011/01/01/happynew-years/

我希望删除example.com/username/之后的所有内容,因此其余列表将如下所示:

blog.example.com/ilovecats/
blog.example.com/fas24
blog.example.com/morg
blog.example.com/whistlermoar/
blog.example.com/punny/
blog.example.com/anniesblog/
blog.example.com/alfva/
blog.example.com/dudewheresmycar/
blog.example.com/mynameisbilly/
blog.example.com/anotherflower/
blog.example.com/naxramas/
blog.example.com/angeleoooo /

我听说正则表达式是这样做的一种方式,所以我现在已经花了好几个小时来讨论这个问题,而且我即将耗尽时间。

有人可以帮助我吗?

(安装了Notepad ++)

2 个答案:

答案 0 :(得分:2)

您可以使用:

(blog.example.com/\w+\/?).*

将其放入查找中并确保在搜索模式中选择“正则表达式”。

在替换中,放:

\1

并替换所有。

答案 1 :(得分:0)

这是要搜索的正则表达式。

^([.\w]+\/\w+\/?).*

这是替代品。

 \1

让我们分解吧。正则表达式看起来像是在呼叫调制解调器,除非你仔细分解它们。

^        only match strings starting at the beginning of a line.
(        begin gathering a bunch of stuff so we can replace it with \1
   [.\w]+   accept a sequence of either dots or characters that appear in words
   \/       accept a / 
   \w+      accept a sequence of characters that can appear in words
   \/?      accept a /, optionally (hence the ?)
)        the end of the parenthesis started above
.*       accept the rest of the string.

请注意,我使用+字符表示重复,因为它匹配一个或多个字符。我可以使用*,并在正则表达式的最后一项中这样做。匹配零次或多次重复。