3

I am making a small / simple IRC client, for a special use case. I would like to strip out the Mirc colour codes other users are typing from their Mirc / other similar clients.

Screenshot :

enter image description here

I have been trying for approx 1hr without success :

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);

Please help, thanks in advance.

EDIT:

this is HelloWorld in red.

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

EDIT 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
sprocket12
  • 5,368
  • 18
  • 64
  • 133
  • Docs: http://www.mirc.com/colors.html – dtb May 12 '12 at 22:04
  • @dtb yes I read that, it uses the 3 ascii code , with format ^CN[M] but I can't manage to strip out the codes no matter what Regex I try. – sprocket12 May 12 '12 at 22:07
  • Make a the string L12HelloWorld convert it to a char-array and post the content of each char in hex or decimal format, then i might be able to help you. – raisyn May 12 '12 at 22:08
  • @youllknow I tried to find what that L char is, but the debugger doesnt help there, I tried to copy and paste it into the c# code but that didnt work either. The best I can do is trust its /x03 as it says everywhere. or /x02 for bold. – sprocket12 May 12 '12 at 22:10
  • 1
    Just run: foreach(char c in s) Console.WriteLine((int)c); where s is your bad string and post the output – raisyn May 12 '12 at 22:27
  • This has already been solved: [https://stackoverflow.com/questions/68968234/do-you-need-to-remove-strip-mirc-colour-format-codes-in-python](https://stackoverflow.com/questions/68968234/do-you-need-to-remove-strip-mirc-colour-format-codes-in-python) – bauderr Sep 09 '21 at 18:23

2 Answers2

3

If you want to strip out ALL BURC codes (bold, underline, reverse and colour), you can also use a regular expression such as the following:

msg = Regex.Replace(msg, @"[\x02\x1F\x0F\x16]|\x03(\d\d?(,\d\d?)?)?", String.Empty);
sprocket12
  • 5,368
  • 18
  • 64
  • 133
Jarmex
  • 679
  • 4
  • 14
  • Your solution almost worked, please see edit 2, the beginning colours are gone, but some weird characters still appear in the ends. – sprocket12 May 12 '12 at 22:48
  • Strange, please could you post the hex/ascii values of those characters like you did with the original? – Jarmex May 12 '12 at 23:03
  • I've edited my code to include the underline (\x1F) since I forgot that in the original – Jarmex May 12 '12 at 23:13
  • Ah yes, what's happening in what you posted is that a final opening colour code (\x03) is used to end the block colour, instead of the standard 'end control codes' character (\x0F). I've updated my snippet so it will also strip these. – Jarmex May 12 '12 at 23:25
0

Try the following, works for you hello world sample

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

Basically just look what the char in your strings are and build your regex up to match any of them 3... strangle L char which is regex \x03 51... number 3 which is in regex \d

@EDIT 3:
The code I posted gives:

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

If you don't want the whitespace and newline you could use .Trim()

 s = new Regex(@"\x03(\d{1,2}(,\d{1,2})?)?").Replace(s, "").Trim();
raisyn
  • 4,514
  • 9
  • 36
  • 55