Skip to main content
February 12, 2010
Question

Url regex

  • February 12, 2010
  • 2 replies
  • 418 views

Is there a way to be able to do the following, lets say i get passed the following url:

htttp://www.amazon.com?type=books&sessionid=1234

And i want to be able to strip it and only get the following:

www.amazon.com

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    February 12, 2010

    To start with, I am assuming there's been a typo, and you mean http. Then you can solve the problem the old-fashioned way.

    Treat the URL as a string of the form:

    http://xxx?yyy

    where xxx is the part you want. You can consider the symbol ? as a delimiter of a list that consists of the 2 elements, http://xxx and yyy. All that is left is for you to strip off http://

    <cfset urlString="http://www.amazon.com?type=books&sessionid=1234">
    <cfset domain = listGetAt(urlString,1,"?")>
    <cfset www = replaceNoCase(domain, "http://", "")>
    <cfoutput>#www#</cfoutput>

    Inspiring
    February 12, 2010

    Yep.

    Have a look at rereplace():

    http://livedocs.adobe.com/coldfusion/8/htmldocs/functions_m-r_35.html#135742

    And how regular expressions work:

    http://livedocs.adobe.com/coldfusion/8/htmldocs/regexp_01.html#1100400

    Or a slightly obtuse way would be to treat the URL like a slash-delimited list, and the domain name would be the second element.

    --

    Adam

    Inspiring
    February 12, 2010

    Or a slightly obtuse way would be to treat the URL like a slash-delimited list, and the domain name would be the second element.

    Of course this only works if you have a file part to your URL, which in your case you do not.

    So you could treat it as a "/?" delimited list instead.

    --

    Adam