Alec
Alec

Reputation:

.Net C# regex for parsing URL parameters

I need to parse a string, such as /a/b/c/d=uno/c=duo.html

into three groups, such as

The parsing rules are:

  1. The first group is everything until "d=" or the whole string if "d=" is not a part of the string.
  2. The second group is everything until "c=" or the rest of the string following the first group if "c=" is not a part of the string.
  3. The third group matches the rest of the string following the second group.

My problem with the following regex (?.+/)?(?d=([^/]+)/)?(?c=(?.*)) is that I don't know how to stop the group when it encounters "d=".

Any help will be appreciated.

Thanks.

Upvotes: 0

Views: 8157

Answers (1)

Matt Hamilton
Matt Hamilton

Reputation: 204289

Is the string you need to parse in the form you supplied, or is it an actual URL with parameters? If it's a URL, you can use System.Web.HttpUtility.ParseQueryString to extract a NameValueCollection containing each parameter and its value.

I've found this useful even in Windows Forms (eg parsing query parameters in ClickOnce deployed applications).

Upvotes: 8

Related Questions