2

Possible Duplicate:
Get individual query parameters from Uri

I have got a URL like this:

http://somedomain.com/website/webpage.aspx?token=123456&language=English

My goal is to extract 123456 from it. There can only be one payment ID in the query-string parameter. What kind of regular expression can I use? I am using C# (.NET) by the way.

Thanks

Community
  • 1
  • 1
Varun Sharma
  • 2,591
  • 8
  • 45
  • 63
  • 5
    Check out [System.Uri](http://msdn.microsoft.com/en-us/library/system.uri.aspx) and [HttpUtility.ParseQueryString](http://msdn.microsoft.com/en-us/library/ms150046.aspx) – nhahtdh Nov 22 '12 at 03:14
  • Possible duplicate of [Get url parameters from a string in .NET](https://stackoverflow.com/questions/659887/get-url-parameters-from-a-string-in-net) – Cœur Jul 14 '18 at 17:56
  • @Cœur - right, thanks for letting us know. – Varun Sharma Jul 16 '18 at 02:11
  • @VVV yet, you didn't add your vote and your question is still open. :) – Cœur Jul 16 '18 at 02:24

4 Answers4

7

Use System.URI class

The Query property of the URI class returns the entire query as a string

Uri bUri = new Uri("http://somedomain.com/website/webpage.aspx
                    ?token=123456&language=English");

var query = bUri.Query.Replace("?", "");

Now query will have the string value "token=123456&language=English"

Then use LINQ to produce a Dictionary from the query attributes

var queryValues = query.Split('&').Select(q => q.Split('='))
                   .ToDictionary(k => k[0], v => v[1]);

You can then access the values as

queryValues["token"]

which will give you 123456

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
6

token=(\d+) should do the trick

To cater for aplhas as well you can do either:

token=([a-zA-Z0-9+/=]+) to explicitly match on the characters you expect. This matches "token=" and then captures all following characters that match the character class, that is, a-z, A-Z, 0-9, +, / and =.

or

token=([^&#]+) to match any character except for the ones you know can finish the token. This matches "token=" and then captures all characters until the first &, # or the end of the string.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Thanks. By the way token can contain A-Z, a-z, 0-9, +, /, = – Varun Sharma Nov 22 '12 at 03:13
  • @VVV: How about `[?&]token=([^&]*)` then? – Cameron Nov 22 '12 at 03:14
  • 1
    Do not use regex to parse urls in C#! – maček Nov 22 '12 at 03:24
  • @Andrew Cooper - Can you explain that regular expression. It works correctly. – Varun Sharma Nov 22 '12 at 03:55
  • 1
    @Macek - My situation is little different and I need to use regular expression to extract this. – Varun Sharma Nov 22 '12 at 03:55
  • @VVV, there's nothing special about the situation you described in your question. If your situation is different than the one you posted, please post your *actual* situation. – maček Nov 22 '12 at 04:04
  • http://somedomain.com/website/webpage.aspx?token=+++++//&language=English, from here I want extract ++++++// but if you use inbuilt .net methods for url parsing -> it automatically converts them to space and / will be converted to something like %3f etc. – Varun Sharma Nov 22 '12 at 04:09
  • Also when a token has a + symbol, I can actually mean + (not a space). Now this is not right because instead of + sign in my URL - I should have had %2b instead. But the URL is not under my control. – Varun Sharma Nov 22 '12 at 04:19
  • @macek - I tested your code. If the payment id is ++++//=, I only get " //". + character is getting converted to space. – Varun Sharma Nov 22 '12 at 04:32
  • 1
    @VVV, oh, so you're saying your URL is not encoded properly? ok :P – maček Nov 22 '12 at 04:40
  • @ Macek - the real problem is that the URL is in incorrect format but our code has no control over it and we just have to deal with it. Thanks for your time. – Varun Sharma Nov 22 '12 at 07:49
  • 1
    @VVV - I've added some explanation to my answer – Andrew Cooper Nov 23 '12 at 03:01
3

Do not use regex for this. C# has built-in methods for this!

var queryString = string.Join(string.Empty, url.Split('?').Skip(1));
System.Web.HttpUtility.ParseQueryString(queryString)

See more details here

Community
  • 1
  • 1
maček
  • 76,434
  • 37
  • 167
  • 198
0

Use

(?:token=)(?<TokenValue>[^&]+)

and in C# get just the value by:

myMatch.Groups["TokenValue"].Value
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Do not use regex to parse urls in C#! – maček Nov 22 '12 at 03:29
  • 2
    @macek I use, and advise one to use the System.URI class as with your answer. But I use and teach how to use Regex to do general parsing. The OP asked for a regex solution; to wit I provided. There are times such as when system.uri or xml link is not available, just as when a limited .Net client profile (as found in Silverlight). I figured the OP was either running a version where URL processing is not provided or running a thin .Net client, or using this as an example to *learn* regular expressions. To that end, I answered in a regex. – ΩmegaMan Nov 23 '12 at 16:05