Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrive a Digit from a String using Regex

Tags:

c#

.net

regex

What I am trying to do is fairly simple, although I am running into difficulty. I have a string that is a url, it will have the format http://www.somedomain.com?id=someid what I want to retrive is the someid part. I figure I can use a regular expression but I'm not very good with them, this is what I tried:

Match match = Regex.Match(theString, @"*.?id=(/d.)");

I get a regex exception saying there was an error parsing the regex. The way I am reading this is "any number of characters" then the literal "?id=" followed "by any number of digits". I put the digits in a group so I could pull them out. I'm not sure what is wrong with this. If anyone could tell me what I'm doing wrong I would appreciated it, thanks!

like image 613
TheMethod Avatar asked Dec 11 '22 21:12

TheMethod


1 Answers

No need for Regex. Just use built-in utilities.

string query = new Uri("http://www.somedomain.com?id=someid").Query;
var dict = HttpUtility.ParseQueryString(query);

var value = dict["id"]
like image 166
L.B Avatar answered Dec 27 '22 19:12

L.B