Simple regex question - match parameter in URL

  • Thread starter Thread starter Warren
  • Start date Start date
W

Warren

Sorry to ask something so elementary, but I'm just not getting it.

suppose I want to match the value of parameter b from a URL like
http://www.abcd.com/?a=1&b=2&c=3

I can use this regex expression:
&b=(.+)&

and then pull the value of group 1.

But suppose the c parameter is not included - the URL is
http://www.abcd.com/?a=1&b=2

Then, of course, the & doesn't match. So I have to replace it with
either nothing, or \b, or \Z, or something equivalent. They all work.

What I *don't* get is how to OR those conditions together. If the
second condition were a character, then I could create a character
class. But even though "&b=(.+)\Z" works, "&b=(.+)[\Z]" does not.

I know there's something simple here I'm not quite getting. Can some
provide a clue? Thanks.
 
I am pretty sure that
&b=(.+)&?
will work.
The question mark means zero or one match

Ethan
 
The following will capture ALL parameters in a QueryString, placing the name
into a group named "name" and the value into a group named "value":

[\?&](?<name>[^&=]+)=(?<value>[^&=]+)

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Newbie question about Regex 8
simple regex? 1
Regex in C# 4
Regex - Matching URLS 2
simple Regex does not match 2
How to convert OST to PDF? Simple Methods 3
regex help 1
Regex Usage, or use Substring? 3

Back
Top