Parse your own string format

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

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."> 

Search should be based on app_major: app_minor: and app_micro

thanks in advance

+4
source share
5 answers

Here is a simple regular expression to select each part with sample code for someone unfamiliar with regular expressions in .NET: (Edited, since the original question was edited to remove asterisks).

 Regex Exp = new Regex(@"(?<=\W)app_major:(?'Number'\d+)"); Match M = Exp.Match(input); if(M.Success) { string Number = M.Groups["Number"].Value; } 

You can simply use 3 different expressions, one for each part that you want to select from the string.

Explanation:

  • The first part (?<=\W) ensures that the match is preceded by a character without a word (in this case, most likely, a comma).
  • The second part of app_major: matches the literal part of the string you are looking for.
  • (?'Number'\d+) is the captured group on which we put Number , where we are trying to match one or more digits \d+ .
+2
source

I did this with LINQ ... maybe not the best way, but it was interesting to figure 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('*') }); 

It produces:

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

Perhaps this could even be made much more elegant :)

EDIT: If you want to make it very easy to access what you want to 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, just give it the key:

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

Note. I tested the LINQ solution and the Regex solution, and the LINQ version is not so different from the speed, but it is a little slower than the published regular expression answers. The regular expression of the response, although it does not clear the data for you, is presented in a simple way. The ToDictionary part really slows it down (from a real-time point of view, although it remains almost nothing), but makes it easier to use.

+5
source

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

The documentation for Regex.Match has a sample showing 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.

+3
source

With regex (?<=:)\d(?=\*\*)

Edited . Since the question is being edited, this is the answer. (?<=(app_major|app_minor|app_micro):)\d*

+3
source

Use regex:

 string regex = @"app_major:(?<major>\d+).*app_minor:(?<minor>\d+).*app_micro:(?<micro>\d+"; RegexOptions options = (RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase); Regex reg = new Regex(regex, options); Match match = reg.Matches(yourString); string major = match.Groups[1].Value string minor = match.Groups[2].Value string micro = match.Groups[3].Value 
+2
source

Source: https://habr.com/ru/post/1311135/


All Articles