tags:

views:

24

answers:

2

I have made this RegEx:

/http:\/\/(.+)?\.?(stackoverflow|superuser|serverfault|stackapps|stackexchange)\.com\/questions\/([0-9]+)\/?.*/i

It needs to match any Stack Exchange website's question. Is this the right RegEx? I have tested it and it works, but I've not tested it on all beta-sites and since I know I'm Satan in the RegEx world, I know there is something wrong (there is always something wrong with my RegExes xD).

Is this one good? Thanks.

+1  A: 

Are you going to be using it against parties that might be trying to trick it? This could match on the querystring of the url, I would think. (http://badguy.com?http://stackoverflow.com/questions).

A better approach might be to use a URL parsing library or function to extract out the important components safely.

Jesse Collins
That doesn't really matter. It's always good if at least a question ID is given (which is the only thing I need from the URL), but I check the RegEx in an if-statement to check if I can strip it out of the URL, see my other question, http://stackoverflow.com/questions/3469714/check-if-a-string-matches-a-regex-and-strip-a-part-of-it-out-for-use for more information.
Time Machine
+1  A: 

Are you sure you want to do this with a regex? I think it would be better if you stored all the existing sites' urls somwhere and use this list instead. When you use a regex you will need to update it anyway once any of the beta sites goes live and chooses its domain...

silvo
I know a subdomain of `stackexchange.com` can be chosen, but can they also choose their own full domain?
Time Machine
Once they leave beta they are on track to becoming full-blown standalone sites with their own domain.
silvo
Mmm.. that's indeed a bit of a problem :/Oh well, it's for an extra, undocumented feature for my website, if nothing happens (doesn't match); people won't know something should happen, and if something does happen (the RegEx does match); it's just extra awesomeness :)
Time Machine
You can create a simple webservice that will return a current list of all stackexchange sites.
silvo