CFLib.org – Common Function Library Project

REGet(str, regex)

Last updated June 06, 2003

author

Raymond Camden

Version: 2 | Requires: CF5 | Library: StrLib

Description:
This UDF will parse a string and return every occurance of a regular expression in an array. If no matches are found, an empty array is returned.

Return Values:
Returns an array.

Example:

<cfset str = "This string contains email addresses like ray@camdenfamily.com and foo@goobers.com. We will use it to email goo@cnn.com and bill@microsoft.com">
<cfset res = REGet(str,"[[:alnum:]_\.\-]+@([[:alnum:]_\.\-]+\.)+[[:alpha:]]{2,4}")>
<cfdump var="#res#">

Parameters:

Name Description Required
str The string to search. Yes
regex The regular expression to search for. Yes

Full UDF Source:

/**
 * Returns all the matches of a regex from a string.
 * Bug fix by  Ruben Pueyo (ruben.pueyo@soltecgroup.com)
 * 
 * @param str      The string to search. (Required)
 * @param regex      The regular expression to search for. (Required)
 * @return Returns an array. 
 * @author Raymond Camden (ray@camdenfamily.com) 
 * @version 2, June 6, 2003 
 */
function REGet(str,regex) {
    var results = arrayNew(1);
    var test = REFind(regex,str,1,1);
    var pos = test.pos[1];
    var oldpos = 1;
    while(pos gt 0) {
        arrayAppend(results,mid(str,pos,test.len[1]));
        oldpos = pos+test.len[1];
        test = REFind(regex,str,oldpos,1);
        pos = test.pos[1];
    }
    return results;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson