2012-05-12 28 views
3

我正在製作一個小型/簡單的IRC客戶端,用於特殊用例。我想刪除其他用戶從其Mirc /其他類似客戶端輸入的Mirc顏色代碼。正則表達式替換Mirc顏色代碼

截圖:

enter image description here

我一直在嘗試了約1小時都沒有成功:

string msg = string.Format("{0}\n", e.Data.Message); 
//msg = Regex.Replace(msg, @"\x03(?:\d{1,2}(?:,\d{1,2})?)?", string.Empty); // Ive tried all sorts here. 
txtActivity.AppendText(msg); 

請幫幫忙,在此先感謝。

編輯:

這是紅色的HelloWorld。

3 
51 
72 
101 
108 
108 
111 
87 
111 
114 
108 
100 
10 

編輯2:

enter image description here

EDIT 3:

3 
48 
49 
44 
49 
53 
32 
49 
46 
32 
73 
110 
32 
116 
104 
101 
32 
78 
97 
109 
101 
32 
111 
102 
32 
31 
65 
108 
108 
97 
104 
31 
44 
32 
116 
104 
101 
32 
77 
111 
115 
116 
32 
66 
101 
110 
101 
102 
105 
99 
101 
110 
116 
44 
32 
116 
104 
101 
32 
77 
111 
115 
116 
32 
31 
77 
101 
114 
99 
105 
102 
117 
108 
31 
46 
32 
3 
10 
+0

文檔:http://www.mirc.com/colors.html – dtb

+0

@dtb是的,我讀了,它採用了3 ascii代碼,格式爲^ CN [M],但無論我嘗試使用哪種正則表達式,我都無法設法去除代碼。 – sprocket12

+0

使字符串L12HelloWorld將其轉換爲char數組並將每個字符的內容以十六進制或十進制格式發佈,然後我可以幫助您。 – raisyn

回答

1

如果要去掉所有BURC代碼(粗體,下劃線,反向和顏色),可以還使用如下正則表達式:

msg = Regex.Replace(msg, @"[\x02\x1F\x0F\x16]|\x03(\d\d?(,\d\d?)?)?", String.Empty); 
+0

您的解決方案几乎可行,請參閱編輯2,開始的顏色消失了,但一些奇怪的字符仍然出現在末尾。 – sprocket12

+0

奇怪的是,請你能發佈這些字符的十六進制/ ASCII碼值,就像你對原始字符所做的那樣嗎? – Jarmex

+0

我編輯了我的代碼以包含下劃線(\ x1F),因爲我忘記了原始的 – Jarmex

0

嘗試以下,適用於您的hello world樣本

s = new Regex(@"\x03(\d{1,2}(,\d{1,2})?)?").Replace(s, ""); 

基本上只是看什麼你的字符串的字符是,建立自己的正則表達式來匹配任何他們 3 ...扼殺大號字符是正則表達式\ X03 51 ...數3這是在正則表達式\ d

@EDIT 3:
我發佈的代碼得到:

" 1. In the Name of Allah, the Most Beneficent, the Most Merciful. \n" 

如果你不希望空格和換行,你可以使用.Trim()

s = new Regex(@"\x03(\d{1,2}(,\d{1,2})?)?").Replace(s, "").Trim();