Blog

4May
2007
Scorpio Adds Two New RegEx Functions

ColdFusion has supported Regular Expressions for a while, but I've always missed a REMatch() function, one that would locate all matches and return them. Over the years several of us have written our own functions to do this, but that's no longer needed - REMatch() and REMatchNoCase() are both in Scorpio.

Comments (10)



  • Ben Nadel

    Sweeeeeet. Yeah, that is huge!

    #1Posted by Ben Nadel | May 4, 2007, 01:37 PM
  • John Farrar

    WHoooOOOOP!!!

    Give us some examples, PLEASE! (Of course I can wait till you are in Lansing, and you can show us that also.)

  • Ben Nadel

    My guess is it would work like this (sorry, I am toooo excited to wait for Ben to say anything ;)):

    <cfset arrMatches = REMatch( TEXT, REG_EX, SCOPE ) />

    <cfloop index="i" from="1" to="#ArrayLen( arrMatches )#">

    Match #i#: #arrMatches[ i ]#

    </cfloop>

    From Ben's description, it looks like it might not have a SCOPE param, but I thought it would be cool.

    #3Posted by Ben Nadel | May 4, 2007, 01:48 PM
  • Ben Forta

    Give it an expression and a string and get back an array of matches. Like this:

    <!--- Find all the URLs in a web page retrieved via CFHTTP --->
    <cfset result = REMatch("https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?", CFHTTP.FileContent)>

    --- Ben

    #4Posted by Ben Forta | May 4, 2007, 01:54 PM
  • Ben Nadel

    Oh cool, so it's more like REFind() rather than REReplace().

    This is totally exciting. I can't wait to see all this in NYC later this month.

    #5Posted by Ben Nadel | May 4, 2007, 02:20 PM
  • Ben Forta

    Ben, yes, similar to JavaScript's Match. Instead of having to do a REFind(), and then loop through the results extracting them all from start position for n chars, this does all the work for you.

    --- Ben

    #6Posted by Ben Forta | May 4, 2007, 02:27 PM
  • Rob Wilkerson

    Genius! This might be my favorite new feature. Hell, it might be my favorite new feature ever introduced. Well, other than CFDUMP. That was beyond genius and will never be surpassed.

  • Terry M

    https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)? Doesn't work with dashes

    eg
    http://www.abc.com/g/xxx-yyy-zzz/123456/v_content/1.jpg

    result
    http://www.abc.com/g/xxx

    #8Posted by Terry M | Nov 9, 2007, 10:05 PM
  • Dan Arsenault

    Hi Ben,
    You said "Over the years several of us have written our own functions to do this, but that's no longer needed", can you give me an example or point me to one that I could reference? I created a string manipulation for CF8, but now need to build back to CF MX 6.1. I am having trouble with performance in all my trials, so any help would be greatly appreciated!

    The script takes 3 seconds in CF8, but when I try to use a different script in CF MX 6.1, it takes alsom 50 seconds. The string input length varies, so I need good performance.

    Thanks,
    Dan

  • Duane Hardy

    I've been struggling with using rematch to add 'target=_blank"' to <a> tags in a string. Can anyone help with the expression?

    #10Posted by Duane Hardy | Jun 11, 2008, 10:43 AM