Valhalla Legends Archive

Programming => Battle.net Bot Development => Topic started by: Smarter on June 12, 2007, 06:03 AM

Title: [C#] Regex String Troubles...
Post by: Smarter on June 12, 2007, 06:03 AM
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?
Title: Re: [C#] Regex String Troubles...
Post by: Chriso on June 12, 2007, 06:38 AM
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.
Title: Re: [C#] Regex String Troubles...
Post by: Smarter on June 12, 2007, 07:08 AM
No, sorry. That doesn't really apply to this at all.
Title: Re: [C#] Regex String Troubles...
Post by: rabbit on June 12, 2007, 07:40 AM
Why do you want to parse the CHAT protocol?  It's dead...
Title: Re: [C#] Regex String Troubles...
Post by: Chriso on June 12, 2007, 08:02 AM
Warnet  ;)
Title: Re: [C#] Regex String Troubles...
Post by: Smarter on June 12, 2007, 08:15 AM
Yep, warnet ;). I'm making a Chat/Warbot for warnet in C#, as .NET Sockets are sexyfast. Can anyone post some useful information ... ?
Title: Re: [C#] Regex String Troubles...
Post by: iago on June 12, 2007, 09:53 AM
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.
Title: Re: [C#] Regex String Troubles...
Post by: Insolence on June 18, 2007, 03:01 AM
I'd just get greedy:
\"(?<Message>.*)\"

Should grab everything between the first and last quote.
Title: Re: [C#] Regex String Troubles...
Post by: 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.
Title: Re: [C#] Regex String Troubles...
Post by: Chriso on June 18, 2007, 08:27 AM
Use \x22  :D

Btw you should read msdn before asking on forums :o
http://msdn2.microsoft.com/en-us/library/4edbef7e(VS.71).aspx
Title: Re: [C#] Regex String Troubles...
Post by: Insolence on June 19, 2007, 08:09 PM
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>.*)"
Title: Re: [C#] Regex String Troubles...
Post by: Smarter on June 19, 2007, 11:03 PM
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....
Title: Re: [C#] Regex String Troubles...
Post by: Insolence on June 21, 2007, 02:18 AM
Well, you need to include the quotes...
Regex rex = new Regex("\"(?<text>.*)\"", RegexOptions.IgnoreCase);

I think that should work.
Title: Re: [C#] Regex String Troubles...
Post by: rabbit on June 21, 2007, 08:29 AM
If you fail to get the regex working, another simple way is to go right to the first quote and left to the second.
Title: Re: [C#] Regex String Troubles...
Post by: Smarter on July 05, 2007, 02:18 AM
Wow, I really need to stop smoking pot, I completely forgot to include the beginning quotes.... it work's perfectly now, thanks.
Title: Re: [C#] Regex String Troubles...
Post by: Chriso on July 05, 2007, 03:43 AM
Good to hear, since you started this thread almost a month ago.
Title: Re: [C#] Regex String Troubles...
Post by: Smarter on July 05, 2007, 04:01 AM
Quote from: Chriso on July 05, 2007, 03:43 AM
Good to hear, since you started this thread almost a month ago.

Yeah, I poked into the source today and reviewed everyones posts, and realized how simple of an error it is... that's the problem with alot of my programs, I always seem to miss the simple things. On another note, what is going on with MirageBot, Chriso?
Title: Re: [C#] Regex String Troubles...
Post by: Chriso on July 05, 2007, 05:25 AM
It should be coming out in a week or two.
Title: Re: [C#] Regex String Troubles...
Post by: Smarter on July 05, 2007, 05:45 AM
I was told you lost the source and all kinds of junk like that?
Title: Re: [C#] Regex String Troubles...
Post by: Insolence on July 05, 2007, 05:52 AM
Quote from: Smarter on July 05, 2007, 02:18 AM
Wow, I really need to stop smoking pot, I completely forgot to include the beginning quotes.... it work's perfectly now, thanks.
No problem.