Capturing text from input

Hi,

I'm new to C# so please, bear with me! I'm trying to write a program which will respond to input from an IRC channel with a response based on that input. This is the start of my else-if statement.

else if (inputLine.EndsWith(":!test"))

What I would like is to be able to capture a word (or more than one word) typed after the "!test" and store t as a string to be used in the message sent back to the IRC channel. I've got all of the sending and recieving of message sorted - it's just this bit which I'm struggling with - is Regex the answer

Thanks,

Martin



Answer this question

Capturing text from input

  • Alan Z

    Hi,

    The below is the sort of command the program recieves:

    :Martinp23!n=martin@host81-159-64-160.range81-159.btcentralplus.com PRIVMSG #wdm
     :!drink orange

    Everything before the PRIVMSG varies depending on the sender of the message, and there can be extra text before the !drink. 

    What I would like to do is output text which reuses the "orange" in the example above, and where the output is called by the prescence of !drink in the message (not neccessarily at the end, but after the "PRIVMSG :" (but then agian - it wouldn't be anywhere else).  Ideally, I'd like the program to be able to reuse the "orange lemon" if there were two words, or any number of words more.  I've tried to use an array - but am a bit stuck with that at the moment:

    string input1;

    input1 = inputLine;

    string[] testArray = input1.Split();

    string[] command = new string[1];

    string[] drink = new string[1];

    Array.Copy(testArray, 3, command, 1, 0);

    Array.Copy(testArray, 4, drink, 1, 0);

    I'm not sure what I'm doing wrong here - but this would only work with one word after the !drink - if I could get it to work in the first place, that is.

    Thanks


  • SankaraNarayanan Nagalingam

    Well, first of all, you are looking for a line which ends with :!test, and then want the words after it. Naturally, there isn't going to be any. So, did you want lines which start with that, or the words before it

    Next, we need to know what exactly defines the text you want to keep. All you say is "a word (or more than one word)" Does it go to the end of the line

    string key = ":!test";

    string inputLine = ":!test blahblahblah";

    string restOfLine;

    if (inputLine.StartsWith(key))
    restOfLine = inputLine.Substring(key.Length); // skip characters equal to the length of key.



  • FRED COLES

    Hi again

    It turns out that that didn't work - I program now takes a long time to parse the

    Match m=reg.Match ((inputLine = reader.ReadLine()));

    line, and then it reports m.Success as False.

    This is my code for that bit:

    Match m=reg.Match ((inputLine = reader.ReadLine()));

    else if (inputLine.Contains("!serve"))

    {

    Regex reg = new Regex(@":( <Address>[^ ]*) PRIVMSG ([^ ]*) :!( <Code>\w+) ( <Data>.*)$");

    Match m=reg.Match ((inputLine = reader.ReadLine()));

    if (m.Success == true)

    {

    //m.Groups[1]; //user details

    string command = m.Groups[2].ToString(); //command

    string food = m.Groups[3].ToString(); //foodstuff

    if (command == "serve")

    {

    Is there something I've done wrong/need to do Thanks


  • RAS.Goss

    This would seem to be a really good place to use a regex. The pattern ":( <Address>[^ ]*) PRIVMSG ([^ ]*) :!( <Code>\w+) ( <Data>.*)$" given the string in your example would break it down as :

    Address => Martinp23!n=martin@host81-159-64-160.range81-159.btcentralplus.com
    Code => drink
    Data => orange



  • kjsteuer

    Hi,

    That worked - thanks! I needed to put an @ at the beginning of the pattern to stop an "Unrecognised escape sequence" error - but aside from that it works well. Thanks.

    Martin


  • A-Style

    Possibly. I'd say you could also use the string trim method and trim off the first 6 characters of the string. I'd first make t the string and then trim it, however.

    Personally, I hate regex, and I avoid using it whenever possible. So far, I've found plenty of ways, including trimming and substrings, to avoid using regex and overcomplicating a simple situation.


  • Capturing text from input