views:

300

answers:

4

I'm looking for some REGEX help Given the following URL: http://news.cnet.com/8301-13924%5F3-10315534-64.html?part=rss&subj=news&tag=2547-1%5F3-0-20

What is the REGEX to obtain the following:

http://news.cnet.com/8301-13924%5F3-10315534-64.html

Thus removing the ? and everything after it

Thanks, B

+1  A: 

This regular expression will do the trick:

^([^?]+)

Just take the second capture group from the match (the first capture group is always the original string itself if it matched).

Andrew Hare
+2  A: 

In ColdFusion you could use regex replace:

myURL = REReplace(myURL,"\?.*$","")

That would leave you with everything before the question mark.

pb
+10  A: 

You can certainly use a regex for this, but it would be more efficient to use

listfirst(theurl, '?')

which finds the first part of a list delimited by question marks.

Ben Doom
good idea.. very coldfusion-y way of doing it!
Kip
+1 for a nice simple solution. The list*() functions in CF can be very powerful when used in non-obvious ways.
Al Everett
I agree, I always look to list functions before regex.
Ryan McIlmoyl
A: 

@Ben Doom: If I'm not mistaken, the #url# variable is a complex object and cannot be treated as a string or list. The way I go about getting everything before the query string is:

<cfset myURL = "http://" & #cgi.HTTP_HOST# & #cgi.SCRIPT_NAME# />
Joe D
Obviously, I didn't mean to attempt this op on the url scope. I unthinkingly used 'url' to mean a variable holding the URL in question.
Ben Doom
Yeah, you know it occurred to me after I wrote my answer that you were referring to a variable with the url stored in it rather than the scope itself. Great answer though, I wouldn't have thought of using that technique.
Joe D