0

I have a string which is a REST query string and it looks like this

//new_requestSet?$select=new_ExternalStatus,new_name&$filter=new_ExternalStatusDirty eq true

I am using a regex class posted here on StackOverFlow. It works well to find some positions in my input string above but I feel like my code to extract the actual values I need is inefficient. Is there a more efficient way using regex instead of IndexOf and SubString?

int fieldPos = StringExtender.NthIndexOf(json, "filter=", 1);
            int firstSpace = StringExtender.NthIndexOf(json, " ", 1);
            int secondSpace = StringExtender.NthIndexOf(json, " ", 2);
            int entityPosEnd = StringExtender.NthIndexOf(json, @"\Set", 1);
            int searchFieldStart = StringExtender.NthIndexOf(json, "=", 2);
            string searchField = json.Substring(searchFieldStart + 1, firstSpace - searchFieldStart - 1);
            string criteria = json.Substring(secondSpace+1);
            string entity = json.Substring(0, entityPosEnd);
Community
  • 1
  • 1
GoBeavs
  • 499
  • 2
  • 10
  • 25

2 Answers2

0

Capture groups should help. http://msdn.microsoft.com/en-us/library/az24scfc.aspx#grouping_constructs

    var regex = new Regex(@"(?<name>[^=?&]+)=(?<value>[^&]+)");
    var results = regex.Matches(request);

    foreach (Match result in results)
    {
        Console.WriteLine("name: {0} - value: {1}", result.Groups["name"].Value, result.Groups["value"].Value);
    }
fsimonazzi
  • 2,975
  • 15
  • 13
0

You can spit you string by "&" first time and receive parameter name/value pairs
Then split second time by "=" each pair and receive distinct value of parameter name/value.

There are a little bit more code you need to write, but no regex at all.