• Welcome to Valhalla Legends Archive.
 

[C#] Regex String Troubles...

Started by Smarter, June 12, 2007, 06:03 AM

Previous topic - Next topic

Smarter

I need assistance creating my Regex String, to parse CHAT, the format TALK is recieved is:

1007 TALK USERNAME 1002 "The Message Here "See""

The text is encased in "'s and I can't split it anyway, without losing portions of the message.

This is my current Regex String, that correctly parses all other CHAT sent to it, except TALK:

Regex rex = new Regex("(?<flags>\\d{4}) (?<opcode>TALK|INFO|ERROR|CHANNEL) \"(?<text>[^\\n]*)\"", RegexOptions.IgnoreCase);

I believe I need something along the lines of:

Regex rex = new Regex("(?<flags>\\d{4}) (?<opcode>TALK|INFO|ERROR|CHANNEL) (?<username>*) (?<somecode>\\d{4} \"(?<text>[^\\n]*)\"", RegexOptions.IgnoreCase);

However, that string does not work....can anyone help?
Since '99

BrutalNet.Net

Chriso

#1
When using the @ character to make a string literal, you can escape a quotation mark with another quotation mark:
@"('|"")"

Don't know if that helps, I got the idea from VB, how you can do """" for a quotation mark.

Smarter

No, sorry. That doesn't really apply to this at all.
Since '99

BrutalNet.Net

rabbit

Why do you want to parse the CHAT protocol?  It's dead...
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Chriso


Smarter

Yep, warnet ;). I'm making a Chat/Warbot for warnet in C#, as .NET Sockets are sexyfast. Can anyone post some useful information ... ?
Since '99

BrutalNet.Net

iago

Which part of the Regex is breaking? I'm guessing the <text> part? Can you post just the text part, and what each bit of it does? I'm not familiar with the C#-style syntax, it's different than what I'm used to.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Insolence

I'd just get greedy:
\"(?<Message>.*)\"

Should grab everything between the first and last quote.

Smarter

Just tried that, doesn't work, only takes what's in the first found quote to the NEXT found quote, messages containing quotes fuck up.
Since '99

BrutalNet.Net

Chriso


Insolence

Quote from: Smarter on June 18, 2007, 04:10 AM
Just tried that, doesn't work, only takes what's in the first found quote to the NEXT found quote, messages containing quotes fuck up.
You must be doing something wrong, because it works for me.

In Rad Regular Expression Designer, I used these values:
Input - "I am quoting someone, "a penny saved is a penny earned".  Eric says, "This works!""
RegEx - "(?<Message>.*)"

Smarter

I tried that, and it returned:

[12:01 AM] <.eVe.HaZe> 1005 TALK .eVe.HaZe 0010 "~.eVe.~ - QaYiN Chat v1.1.2"

                case "1005": //TALK
                    main.AddChat("<" + stSplt[2] + "> " + GetTalk(data), Color.White);
                    break;

My Talk Parse.


        public string GetTalk(string str)
        {
            Regex rex = new Regex("(?<text>.*)", RegexOptions.IgnoreCase);
            Match m = rex.Match(str);
            string messageText;
            if (m.Success)
            {
                return messageText = m.Groups["text"].Value;
            }
            else
            {
                return str;
            }
        }


My GetTalk function, as you can see, it's still not parsing it correctly....
Since '99

BrutalNet.Net

Insolence

Well, you need to include the quotes...
Regex rex = new Regex("\"(?<text>.*)\"", RegexOptions.IgnoreCase);

I think that should work.

rabbit

If you fail to get the regex working, another simple way is to go right to the first quote and left to the second.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Smarter

Wow, I really need to stop smoking pot, I completely forgot to include the beginning quotes.... it work's perfectly now, thanks.
Since '99

BrutalNet.Net