RegEx needed to parse a URL

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm building a test platform that includes parsing a set of URL's. This is
not part of a ASP.Net project. Otherwise I'd be posting it in that area.

Given a URL of
http://Somthing/Somthing.asp?parm1=val1&parm2=val2&parm3=val3

I'm trying to create an expression that gives me:
parm1=val1
parm2=val2
parm3=val3

As either distinct group values (Match (str).Groups) or as captures of a
group (Match (str).Groups[1].Captures)

I've tried @".*\?(.*&)*?(.*)"
but this gives me the first two parms as a single string with the last parm
being distinct. I've also tried other variations but this is the closest I've
gotten.

TIA,
Dave
 
Why not just strip before ? and split it on the &. That will give you an
array containing what your after.
 
While I realise its not RegEx (they make my head ache), something like this
might work:
Uri uri = new
Uri(@http://Somthing/Somthing.asp?parm1=val1&parm2=val2&parm3=val3);
string[] querystrings = uri.Query.Replace("?", "").Split('&');
 
(?<=[^?\s\n]+\?)([^\s\n]+)

This uses a positive look-behind to ensure that a match (any text that has
no spaces or line breaks in it) is preceded by a URL (any text that does not
contain a line break, space, or question mark), followed by a question mark.
This effectively divides the matches by URL. So, each match will be a single
Query String.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.
 
I'm so used to parsing with regular expressions I forget about normal string
manipulation. :o

Thanks

Stuart Irving said:
While I realise its not RegEx (they make my head ache), something like this
might work:
Uri uri = new
Uri(@http://Somthing/Somthing.asp?parm1=val1&parm2=val2&parm3=val3);
string[] querystrings = uri.Query.Replace("?", "").Split('&');


Dave said:
I'm building a test platform that includes parsing a set of URL's. This is
not part of a ASP.Net project. Otherwise I'd be posting it in that area.

Given a URL of
http://Somthing/Somthing.asp?parm1=val1&parm2=val2&parm3=val3

I'm trying to create an expression that gives me:
parm1=val1
parm2=val2
parm3=val3

As either distinct group values (Match (str).Groups) or as captures of a
group (Match (str).Groups[1].Captures)

I've tried @".*\?(.*&)*?(.*)"
but this gives me the first two parms as a single string with the last
parm
being distinct. I've also tried other variations but this is the closest
I've
gotten.

TIA,
Dave
 
Back
Top