-1

Need to parse a string having format like this -

"xxx.xxx.xxx.xxx ProxyHost=prod.loacl.com ProxyUser=test ProxyPas=tes@123 ProxyPort=1809".

Need to split or parse in such a manner that I get "prod.loacl.com" "test" "tes@123" "1809" in some strings and if any of parameters is not defined like ProxyPas then it should be null.

We need to ignore the IP addr xxx.xxx.xxx.xxx it will be always concatenated.

Do we have split or use some list to get this done...which is the best possible way to extract this information and why?

Note: Input string can change except ProxyHost parameter, user may not input the ProxyPass etc.

cjoshi
  • 31
  • 6
  • 2
    Seems like a job for regex. If you add the regex tag you will likely have this answered in sub milliseconds.. – TheGeneral Sep 01 '21 at 09:29
  • 1
    I agree with @TheGeneral. Also, it shares some structure with `args` passed to `program.cs`. Therefore you may find [this](https://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c) post interesting. – smoksnes Sep 01 '21 at 09:32
  • Thanks @TheGeneral but input shall change as mentioned in Edited . – cjoshi Sep 01 '21 at 11:19

3 Answers3

2

If you assume that format of the input string will not change, you can do something like this:

string inputString = "xxx.xxx.xxx.xxx ProxyHost=prod.loacl.com ProxyUser=test ProxyPas=tes@123 ProxyPort=1809";
string[] eachPart = inputString.Split(" ");
for(int i = 1; i < eachPart.Length; i++) // Skip the IP address
{
   string[] partData = eachPart[i].Split("=");
   string dataName = partData[0];
   string dataValue = partData[1];
   // do something with dataName and dataValue
 }

However, if input string can change its format you should add some additional logic to this code.

Dušan
  • 269
  • 1
  • 11
1

Use regex with groups for this, sample:

        var myString = "xxx.xxx.xxx.xxx ProxyHost=prod.loacl.com ProxyUser=test ProxyPas=tes123 ProxyPort=1809";
        var regex = new Regex(@"ProxyHost=([^\s]+) ProxyUser=([^\s]+) ProxyPas=([^\s]+) ProxyPort=(\d+)");

        var match = regex.Match(myString);
        while(match != null && match.Success)
        {
            int i = 0;
            foreach(var group in match.Groups)
            {
                Console.WriteLine($"Group {i}: Value:'{group}'");
                i++;
            }
            match = match.NextMatch();
        }

now you can match the groups to your properties.

Daniel W.
  • 938
  • 8
  • 21
  • Does match = match.NextMatch(); required when the input string is not repetitive? Also how about null values? – cjoshi Sep 01 '21 at 10:18
  • NextMatch(); is only required if there is more than one line in string. For null values you should add a check. – Daniel W. Sep 01 '21 at 11:50
0

One of the possible approaches is to do this Regular Expression:

([^=]+?)\=((\"[^"]+?\")|([^ ]+))

on the whole string. This allows variable input like this:

variable="this has spaces but still is recognized as one"

Problem is that seems like the variable content will be in either 3rd or 4th Group of such match, according to online regex testers, depends on if it has quotes or simply one string - must have more elegant way to do this, but can't come up with any now.

You can check this document to understand more about C#'s regexp groups: Match.Groups

You will have to deal with null inputs accordingly, when you are putting the content into your C# variable.

Xiang Wei Huang
  • 336
  • 1
  • 9