CFLib.org – Common Function Library Project

RESplit(str, regex)

Last updated January 29, 2002

Version: 2 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
This UDF will split a string into an array based on the regular expression passed in. If the regex is not found, the entire string is returned in the first element of the array.

Return Values:
Returns an array.

Example:

view plain print about
<cfset str = "This is the end of the sentence. This is the second sentence, it's not quite as interesting.">
<cfset res = RESplit(str,"[\.[:space:],]+")>
<cfdump var="#res#">

Parameters:

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

Full UDF Source:

view plain print about
<cfscript>
/**
 * Splits a string into arrays based on a regex.
 * Fix for missing end item by Thomas Muck (tommuck@basic-drumbeat.com)
 * 
 * @param str      The string to search. 
 * @param regex      The regular expression to split on. 
 * @return Returns an array. 
 * @author Raymond Camden (tommuck@basic-drumbeat.comray@camdenfamily.com) 
 * @version 2, January 29, 2002 
 */

function RESplit(str,regex) {
    var results = arrayNew(1);
    var test = REFind(regex,str,1,1);
    var pos = test.pos[1];
    var oldpos = 1;
    if(not pos) results[1] = str;
    while(pos gt 0) {
        arrayAppend(results,mid(str,oldpos,pos-oldpos));
        oldpos = pos+test.len[1];
        test = REFind(regex,str,oldpos+1,1);
        pos = test.pos[1];
    }
        //Thanks to Thomas Muck
        if(len(str) GT oldpos) arrayappend(results,right(str,len(str)-oldpos + 1));
    return results;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Troy Pullis Troy Pullis added
firstXDayOfMonth
a while ago

Henry Ho Henry Ho added
arrayMap
a while ago

Henry Ho Henry Ho added
queryGetRow
a while ago

Tony Felice Tony Felice added
getRowFromQuery
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 44 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson