Parsing windows 'ipconfig /all' output

前端 未结 3 680
天命终不由人
天命终不由人 2021-01-27 03:40

I am having a bit of trouble parsing the output of \'ipconfig /all\' using a regex. Currently I am using RegexBuddy for testing, but I want to use the regex in C#.NET.

M

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-27 04:21

    Why use regex? Your input is in simple key-value format. Use something along the lines of

    foreach (var line in lines)
    {
       var index  = line.IndexOf (':') ;
       if (index <= 0) continue ; // skip empty lines
    
       var key   = line.Substring (0,  index).TrimEnd (' ', '.') ;
       var value = line.Substring (index + 1).Replace ("(Preferred)", "").Trim () ;
    }
    

提交回复
热议问题