2011-01-19 20 views
1

我有一個像正則表達式刪除重複的URL

google.com 
google.com/1 
google.com/2 
google.com/3 
google.com/4 
google.com/5 
google.com/6 
yahoo.com 
yahoo.com/1 
yahoo.com/2 
yahoo.com/3 
yahoo.com/4 
yahoo.com/5 
yahoo.com/6 

有多個URL列表如何刪除第一個3項保持google.com/36和同樣爲雅虎?

+1

在你有什麼形式的條目?一個集合,一個字符串,別的東西?此外,爲什麼您的問題標題爲重複的網址,我無法在列表中看到任何重複的網址。 – 2011-01-19 10:30:41

+0

@Tim你的編輯意味着不同的答案 – 2011-01-19 10:39:15

+1

@Jader Dias:我剛剛重新格式化了它,因爲原始帖子中的換行符不可見,因爲詹姆斯沒有使用樣例的「{}」按鈕。 – 2011-01-19 10:53:43

回答

0

在C#:

resultString = Regex.Replace(subjectString, 
    @"^  # Start at the start of a line 
    [^/\r\n]+ # Match one or more characters except/
    $   # Match the end of the line, thereby ensuring that 
       # the entire line does not contain a/
    (?:  # Match the following group: 
    \r\n  # - a linebreak 
    .*  # - an entire line 
    ){2}  # exactly twice 
    \r\n  # Match the final line break", 
    "", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace); 

得到的字符串:

google.com/3 
google.com/4 
google.com/5 
google.com/6 
yahoo.com/3 
yahoo.com/4 
yahoo.com/5 
yahoo.com/6 
0

我不知道正則表達式是這個最好的辦法。但這裏也無妨:

s/(google.com[\s/\d]*){3}// 
s/(yahoo.com[\s/\d]*){3}// 

的正則表達式是置於斜槓和前置s是替代在vi符號