1

I have the following url

http://example.com/pa/TaskDetails.aspx?Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643&Assn=2A992D9C-C511-E611-80E4-005056A13B51

I need to extract the A5AF5C0D-648A-4892-A995-CDA8013F2643 portion of the url parameter:

Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643

This can be in the middle or at the end of the url. I cannot guarantee the position of it. But i always starts with Proj= and end with &. The string between this is what i want. How can i grab this within C#?

STORM
  • 4,005
  • 11
  • 49
  • 98

4 Answers4

2

It seems that you are trying to retrieve the IDFA from a url address. I think you can easily do that by applying regular expressions to the url string. For example, the following:

[0-9a-fA-F]{8}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][[0-9a-fA-F]{12}

Picks up every valid IDFA when applied to the URL string. You can add conditions for the head and tail of the IDFA to retrieve exactly what you are looking for:

Proj=[0-9a-fA-F]{8}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][[0-9a-fA-F]{12}&

You can test the above Regex (regular expression) syntax on one of the many free online Regex applets (e.g. https://regex101.com/)

To apply Regex to your code, please see the following thread: c# regex matches example

Community
  • 1
  • 1
kod5kod
  • 61
  • 4
1

One solution:

string param = HttpUtility
.ParseQueryString("http://example.com/pa/TaskDetails.aspx?Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643&Assn=2A992D9C-C511-E611-80E4-005056A13B51")
.Get("Proj");   
Luis Teijon
  • 4,769
  • 7
  • 36
  • 57
ramden
  • 798
  • 1
  • 9
  • 25
1
string som = "http://example.com/pa/TaskDetails.aspx?Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643&Assn=2A992D9C-C511-E611-80E4-005056A13B51";
int startPos = som.LastIndexOf("Proj=") + "Proj=".Length + 1;
int length = som.IndexOf("&") - startPos;
string sub = som.Substring(startPos, length); //<- This will return your key

This should do it.

mm8
  • 163,881
  • 10
  • 57
  • 88
crellee
  • 855
  • 1
  • 9
  • 18
1

You may need to create a Uri and pass the value of its Query property to the HttpUtility.ParseQueryString method:

string value = HttpUtility.ParseQueryString(new Uri("http://example.com/pa/TaskDetails.aspx?Proj=A5AF5C0D-648A-4892-A995-CDA8013F2643&Assn=2A992D9C-C511-E611-80E4-005056A13B51").Query)["Proj"];

The method is defined in System.Web.dll by the way so you need to add a reference to this one.

mm8
  • 163,881
  • 10
  • 57
  • 88