Simple regex question - match parameter in URL

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.
 
E

Ethan Strauss

I am pretty sure that
&b=(.+)&?
will work.
The question mark means zero or one match

Ethan
 
K

Kevin Spencer

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

regex replace pipe character 3
help with lastname regex in c# code 3
Prb Match RegEx 2
Regex Help 2
Adding a literal to a regex match 1
Regex question 6
Regex OR match ??? 1
Regular Expressions 4

Top