Pulling a URL from a string using ReMatch?

Posted on 2011-04-19
Medium Priority
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 http://www.experts-exchange.com">

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
  • 2
LVL 52

Expert Comment

ID: 35427053
<cfif arrayLen(theURL)>
      no matches found
LVL 52

Accepted Solution

_agx_ earned 2000 total points
ID: 35427078
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 http://www.experts-exchange.com">
<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

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

Much appreciated!

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

850 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