tags:

views:

43

answers:

4

Hi all, I am looking for a regex query that would allow me to retrieve a value from a string here are examples of my string:

home.aspx?StudyID=0020101&aa=72
randompage.aspx?studyid=3023603&aa=40
myconfig.aspx?studyid=0021600&aa=40

I need to get the numerical value of the 'studyid' variable, please note that the name of the page will change so simply doing the substring and counting char spaces didn't work

I unfortunately cannot use request.querystring method as this string is stored in the database and a select statement will be used for running this regex query

Thanks

A: 
/studyid=([^&]*)&/i

The first group will contain the variable.

tloflin
A: 

[studyid=]\d+

That will grab =0020101

online regex testing tool

http://www.gskinner.com/RegExr/

george9170
I don't know what syntax you're using but generally square brackets represent character class. your regex will also match `i=2`, and you disregard case-sensitivity as well
SilentGhost
I posted the regEx editor I used. You are right and the OP needs to clarify his input a little better.
george9170
@george: and I posted proof that your regex is wrong.
SilentGhost
So you want me to post proof that your regex is wrong? if so studyid=1 will blow up your regex.I dont want this to turn into some sort of flame war. According to the regex website I used. My regex should only return what i stated in my post. If you are using a different engine then it will behave differently. But the engine I am using states otherwise.
george9170
@george: well, that was just low
SilentGhost
+1  A: 
/studyid=(\d{7})/i
SilentGhost
I would recommend not using the {7} quantifier. I wouldn't recommend this because he hasn't stated how long a studyID could be. If it is longer then 7 chars then your regex blows up.
george9170
@george: if the value is any different than that, then OP needs to provide relevant examples.
SilentGhost
Yes generally it is 7 chars, so should be okThanks
Alex
+1  A: 

Use can use parenthesis to capture values in regex.

Therefore, you can match the string to studyid=(\d+) and get the value using $1.

Pran