« Client side HTTP Redirect syntax | Main | Installer & Setup Projects, Part 2 »

Query string parsing using Regex Captures

Repeated captures are accessed through Groups[i].Captures[j].

Using non-capturing groups (?:) simplifies access by eliminating unused groups from the results.

 

Match m = Regex.Match(uri, @"(?'url'[^\?]+)(?:\?(?:(?'arg'[^=&]+)=(?'value'[^&]+)&?)*)?");

 

StringBuilder sb = new StringBuilder();

sb.Append("<html><body ><form id='f' method='post' action='");

sb.Append(m.Groups["url"].Value);

sb.Append("'>\n");

 

for (int i=0; i < m.Groups["arg"].Captures.Count; i++)

      sb.AppendFormat("<input type='string' name='{0}' value='{1}'>\n"

            , m.Groups["arg"].Captures[i].Value

            , System.Web.HttpUtility.UrlDecode(m.Groups["value"].Captures[i].Value));

 

sb.Append("<input type='submit' name='Do' value='It'></form></body></html>");

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)