Pulling a URL from a string using ReMatch?

Posted on 2011-04-19
Last Modified: 2012-08-14
Hi Folks,

I'm trying to pull a url from an inputted string (containing other text) - as far as I can tell, the best way of doing this is using Coldfusion ReMatch.

Here's where I've got so far;

<cfset msg="This is a sample message containing a URL">

The msg variable could contain text either side of the url.

So far, I've got this far;

<cfset theurl = #REMatchNoCase("https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?",msg)#>

Which appears to work (or, at least, doesn't product an error!).

However, I'm struggling with returning the url to the page.  I understand that my RegEx will produce an array, however I can figure out a way of getting to it.  I've tried ListFirst without success.

Any pointers much appreciated!
Question by:RedBullLee
    LVL 51

    Expert Comment

    <cfif arrayLen(theURL)>
          no matches found
    LVL 51

    Accepted Solution

    I understand that my RegEx will produce an array

    Right. The array will contain 0 or more elements, depending on how many matches it found. So use arrayLen() to determine if *any* matches were found.  

    If you want to output all of the matches found, cfloop through the array

    <cfset msg="This is a sample message containing a URL">
    <cfset theurl = #REMatchNoCase("https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?",msg)#>
    <cfif arrayLen(theURL)>
    	<cfloop array="#theURL#" index="x">
    	no matches found

    Open in new window


    Author Comment

    Thanks _agx_ - for some reason I was under the impression that wouldn't work, but it indeed does.

    Much appreciated!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math ( While this method is not o…
    CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now