Reputation: 1967
I'm working with the IRC-protocol, and I am trying to interpret the server messages. If I for instance got the following string:
":USERNAME!~IP PRIVMSG #CHANNELNAME :MESSAGE"
How can I use string.StartsWith if I don't know the variables: USERNAME, IP, CHANNELNAME or MESSAGE?
I would like to do something like this: (I'm aware that this doesn't work)
if(MessageString.StartsWith(":*!~* PRIVMSG #*"))
Upvotes: 0
Views: 166
Reputation: 9837
Try something like this, using the Regex
class.
var regex = new Regex(
@":(?<userName>[^!]+)!~(?<ip>[^ ]+) PRIVMSG #(?<theRest>[\s\S]+)");
var match = regex.Match(MessageString);
if (match.Success)
{
var userName = match.Groups["userName"].Value;
var ip = match.Groups["ip"].Value;
var theRest = match.Groups["theRest"].Value;
// do whatever
}
I would also take a look at the MSDN page for regular expressions in .Net.
Upvotes: 0
Reputation: 186748
You may try using regular expressions:
http://msdn.microsoft.com/en-us/library/az24scfc.aspx
// Check this regular expression:
// I've tried to reconstruct it from wild card in the question
Regex regex = new Regex(@":.*\!~.* PRIVMSG \#.*");
Match m = regex.Match(":USERNAME!~IP PRIVMSG #CHANNELNAME :MESSAGE");
if (m.Success) {
int startWith = m.Index;
int length = m.Length;
...
}
Upvotes: 1
Reputation: 116
Try using a separator after the words you don't know and parse a string from the main one containing only the message.
Upvotes: -1
Reputation: 12866
I would not use StartsWith. I suggest to parse the string by e.g. splitting it into tokens. That way you can check wether the PrivMsg string is contained in the token-List.
There might be libraries allready to parse IRC Messages. Have you checked https://launchpad.net/ircdotnet?
Upvotes: 2