Search code examples
c#regexstringirc

Regex Specific Search lines in any order and may or may not exist


Straight to the point. I am working on an IRC bot, and I am at a brick wall I have been pounding my head on since 4am this morning.

I am trying to sort The IRC Raw 005 (IS_SUPPORTED) strings specifically in regex groups. An example string looks like this.

Nickname MAXTARGETS=20 WALLCHOPS WATCH=128 WATCHOPTS=A SILENCE=15 MODES=12 CHANTYPES=# PREFIX=(qaohv)~&@%+ CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTGZ NETWORK=Network CASEMAPPING=ascii EXTBAN=~,qjncrRa ELIST=MNUCT

I have cleaned up the rest of the packet to this point, so this line is exactly what I am working with, though it may have other fields not shown in my example. I have strings that are all named after each of the possible fields that can be in this packet. I wish to set each of the strings, ints and bools to each of the resulting information, sorted by regex groups in C#

To be more clear,

  • My public int maxtargets would be set to the result of regex group <maxtargets>
  • My public bool wallchops would be set to true, if the regex group <wallchops> returns itself.
  • My public string chantypes would be set to the result of regex group <chantypes>

I am having trouble assembling the regex search string that searches and matches a field and value, in the situations that a field may not exist, and the fields may be in a completely different order.

I hope I am being clear with this and will fill in any blanks I have forgotten.


Solution

  • A friend helped me with this finally and we worked out how to successfully match my above string, weather it has a field or not and no matter that order it may be in.

    (?:(?:SILENCE=(?<silence>\d+)\s?)|(?:MODES=(?<modes>\d+)\s?)|(?:CHANTYPES=(?<chantypes>\S+)\s?)|(?:PREFIX=(?<prefix>\S+)\s?)|(?:MAXTARGETS=(?<maxtargets>\d+)\s?)|(?:WATCH=(?<watch>\d+)\s?)|(?<wallchops>WALLCHOPS)|(?:NETWORK=(?<network>\w+)\s?)|(?:CASEMAPPING=(?<casemapping>\w+)\s?)|(?:CHANMODES=(?<chanmodes>\S+)\s?)|(?:EXTBAN=(?<extban>\S+)\s?)|(?:ELIST=(?<elist>\w+)\s?)|(?:WATCHOPTS=(?<watchopts>\S+)\s?)\s?)
    

    Yes, I realize this is a huge Regex Search string and it will return the lines as groups as I wanted. I also realize this isn't the best idea in practice. This search line would grow to massive size and can, and probably would create a vast amount of overhead and wasted CPU time to execute.

    SJ, I thank you greatly for your feedback, I will put your suggested code to use and get this working in that manner, with some minor changes of course.