2011-05-02 52 views
1

我需要正則表達式來刪除重複的使用語句。Visual Studio multiline reg-exp刪除重複的使用語句

using NS.SN.Services.Common; 
using NS.SN.Data.BaseRegistry; 
using NS.SN.Data.DataContracts.Registry; 
using NS.SN.Services.Common; <-- this one must go 
using NS.SN.Services.DataContracts; 
using System; 
using System.Collections.Generic; 
using System.Transactions; 
using CodeEnums = NS.SN.Data.DataContracts.Enums.CodeEnums; 
using System.Linq; 

問題是這個reg-exp不起作用,因爲([。:b] * \ n)*只匹配第一個新行字符。我可以修復這個reg-exp嗎?

{:b *的使用:bNS.SN.Services.Common;([.: B] * \ n)的*}使用:bNS.SN. \服務\共同;:B * \ n

回答

2

以此作爲 「查找內容」:

{:b*using:bNS\.SN\.Services\.Common;(.*\n)*}using:bNS\.SN\.Services\.Common;:b*\n 

並將此作爲 「替換爲」:

\1 

UPDATE:
我完全錯過了字符組內的點:
[.:b]*將匹配點或冒號或字母b,因爲它在字符類內!你的意思是(.|:b)*,其中:b是多餘的,因爲它包含在「任何字符」(.)內,所以它應該是.*。查看上面更新的正則表達式。

+0

有什麼區別bettwen [:b。\ n] *和[^ \ n \ n] *?順便說一句 - 它非常非常慢! – 2011-05-02 16:02:56

+0

':b'只匹配空格或製表符。 '[^ \ n]'匹配任何不是換行符的東西。你的主要問題是,你沒有匹配所有使用中間的人。 – 2011-05-02 16:04:09

+0

爲什麼?我告訴 - 多次匹配char,space或newline [。:b \ n] *。或者這不是REGEXP的工作原理嗎? – 2011-05-02 16:05:17