Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse my proprietary string format

Tags:

string

c#

extract

How can we get the numbers 7 and 4 and 5 from the following string:

MODULE potmtpMAIN main <info: "Enterprise Optimizer 7.4 for COR Technology 5.5 --    
Advanced Solver Edition", url:"http://EO.riverlogic.com", url_menu:"EO Online...",  
app_id:"EOAS",app_name:"Enterprise Optimizer AS", app_major:7,  
app_minor:4,app_micro:5,app_copyright:"\251 1996-2010 River Logic Inc.\r\nAll 
Rights Reserved."> 

The search must be based on app_major: app_minor: and app_micro

Thank You in advance

like image 250
MicroSumol Avatar asked Dec 07 '22 03:12

MicroSumol


2 Answers

I did it using LINQ... might not be the best way but it was fun to figure it out:

string test = "<info:\"Enterprise Optimizer 7.4 for COR Technology 5.5 -- Advanced Solver Edition\", url:\"http://EO.riverlogic.com\", url_menu:\"EO Online...\", app_id:\"EOAS\",app_name:\"Enterprise Optimizer AS\", **app_major:7**, **app_minor:4**,**app_micro:5**,app_copyright:\"251 1996-2010 River Logic Inc.\r\nAll Rights Reserved.\">";

var result = test.Split(',').
    Select(p => p.Trim().Split(':')).
    Where(i => i[0].Trim().StartsWith("**app_")).
    Select(r => new { Key = r[0].Trim('*'), Value = r[1].TrimEnd('*') });

Produces:

result = {{Key = "app_major", Value = "7"},
          {Key = "app_minor", Value = "4"}, 
          {Key = "app_micro", Value = "5"}}

It could probably even be done much more elegantly :)

EDIT: If you want to make it very simple to access what you want do:

var result = test.Split(',').
    Select(p => p.Trim().Split(':')).
    Where(i => i[0].Trim().StartsWith("**app_")).
    Select(r => new { Key = r[0].Trim('*'), Value = r[1].TrimEnd('*') }).
    ToDictionary(k => k.Key, v => v.Value);

Then to get the value out just give it the key like this:

var version = result["app_major"] ?? "Not Found";

Note I have tested the LINQ solution vs the Regex solution and the LINQ version is not that much of a difference in terms of speed but it is a bit slower than the regex answers posted. The regex answer though do not clean the data for you and present it in a easy to use manner. The ToDictionary part really slows it down (in terms of real time though it is almost nothing still) but it does make the results easier to use.

like image 161
Kelsey Avatar answered Dec 28 '22 12:12

Kelsey


Just do a Regex, something like app_m....:\d might work, but I'm not sure (I usually use one of the tools like RegexBuilder to make them).

The documentation for Regex.Match has a sample to show you how to write the actual code to use the regex:

http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx

Edit: Something like this might work:

Match m = Regex.Match(mystring, "app_major:\d{1-2}");
if(m.Success)
{
     string appMajor = m.Value.SubString(m.Value.IndexOf(":"));
}

If I remember correctly the \d{1-2} bit should mean that you want to get one or two digits.

like image 33
Hans Olsson Avatar answered Dec 28 '22 11:12

Hans Olsson