Reputation: 1979
I have been spending too much time trying to extract this. I have the following message and I want to extract the data part of the message "data {{..... }}" before the arguments. I cannot rely on the order of the messages so arguments result and command could be seen in any order in the message.
invokeid 3 data {{GainID|Value(dB)} {0|0} {1|18} {2|17} {3|-1} {4|-255} {5|0} {6|12} {7|11} {8|10} {9|9} {10|8} {11|7} {12|6} {13|5} {14|4} {15|3} {16|2} {17|1} {18|0} {19|-1} {20|-2} {21|-3} {22|-4} {23|-5} {24|-6} {25|-7} {26|-8} {27|-9} {28|-10} {29|-11} {30|-12} {31|-13} {32|-14} {33|-15} {34|-16} {35|-17} {36|-18} {37|-19} {38|-20} {39|-21} {40|-22} {41|-23} {42|-24} {43|-25} {44|-26} {45|-27} {46|-28} {47|0}} arguments {{ DisplayGain -1 }} result 0 command OAMPCMD IPaddress 0.0.0.0
This is want I want to parse
data {{GainID|Value(dB)} {0|0} {1|18} {2|17} {3|-1} {4|-255} {5|0} {6|12} {7|11} {8|10} {9|9} {10|8} {11|7} {12|6} {13|5} {14|4} {15|3} {16|2} {17|1} {18|0} {19|-1} {20|-2} {21|-3} {22|-4} {23|-5} {24|-6} {25|-7} {26|-8} {27|-9} {28|-10} {29|-11} {30|-12} {31|-13} {32|-14} {33|-15} {34|-16} {35|-17} {36|-18} {37|-19} {38|-20} {39|-21} {40|-22} {41|-23} {42|-24} {43|-25} {44|-26} {45|-27} {46|-28} {47|0}}
And it is done with the following code:
string ret = String.Empty;
Regex regEx = new Regex("data {{.*}}");
Match regExMatch = regEx.Match(iqMessage);
if (!regExMatch.Success)
throw new IqScriptControlMessageParseException(String.Format("Could not find {0}.", DATA));
ret = regExMatch.Value.Substring(DATA.Length).Trim();
I was not able to successfully build a regex that works your help would be appreciated. I am about to code the extraction manually if you know what I mean...
Thanks
Upvotes: 1
Views: 334
Reputation: 66614
Use a non-greedy match (.*?
instead of .*
), and escape the curlies (they have special meaning in regular expressions):
Regex regEx = new Regex(@"data \{\{.*?\}\}");
Upvotes: 3