Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 11:43
Show Gist options
  • Save bennadel/9760124 to your computer and use it in GitHub Desktop.
Save bennadel/9760124 to your computer and use it in GitHub Desktop.
RESplit() - Splitting Strings With Regular Expressions In ColdFusion
<cffunction
name="reSplit"
access="public"
returntype="array"
output="false"
hint="I split the given string using the given Java regular expression.">
<!--- Define arguments. --->
<cfargument
name="regex"
type="string"
required="true"
hint="I am the regular expression being used to split the string."
/>
<cfargument
name="value"
type="string"
required="true"
hint="I am the string being split."
/>
<!--- Define the local scope. --->
<cfset var local = {} />
<!---
Get the split functionality from the core Java script. I am
using JavaCast here as a way to alleviate the fact that I'm
using *undocumented* functionality... sort of.
The -1 argument tells the split() method to include trailing
parts that are empty.
--->
<cfset local.parts = javaCast( "string", arguments.value ).split(
javaCast( "string", arguments.regex ),
javaCast( "int", -1 )
) />
<!---
We now have the individual parts; however, the split()
method does not return a ColdFusion array - it returns a
typed String[] array. We now have to convert that to a
standard ColdFusion array.
--->
<cfset local.result = [] />
<!--- Loop over the parts and append them to the results. --->
<cfloop
index="local.part"
array="#local.parts#">
<cfset arrayAppend( local.result, local.part ) />
</cfloop>
<!--- Return the result. --->
<cfreturn local.result />
</cffunction>
<!--- ----------------------------------------------------- --->
<!--- ----------------------------------------------------- --->
<!--- ----------------------------------------------------- --->
<!--- ----------------------------------------------------- --->
<!--- Create a list of values in which some are empty. --->
<cfset womenList = ",Katie,,Jill,Sarah," />
<!--- Split this list, using the comma as our pattern. --->
<cfset women = reSplit( ",", womenList ) />
<!--- Output the resultant collection. --->
<cfdump
var="#women#"
label="reSplit() Women"
/>
<!--- Define the tab for our field delimiter. --->
<cfset tab = chr( 9 ) />
<!--- Define our tab-delimited data. --->
<cfsavecontent variable="csvData">
<cfoutput>
Name#tab#Age#tab#Hair
Katie#tab#29#tab#Brown
<!--- Totally empty row. --->
Jill#tab##tab#Brown
#tab##tab#
Sarah#tab#33#tab#
</cfoutput>
</cfsavecontent>
<!---
Remove non-relevant white space - ie. remove the leading or
trailing line breaks, but do not remove any TABS.
--->
<cfset csvData = reReplace(
csvData,
"^[\r\n]+|[\r\n]+$",
"",
"all"
) />
<!--- Get the rows using the line breaks. --->
<cfset rows = reSplit( "\r\n?|\n", csvData ) />
<!---
Now that we have the rows, loop over them and split each row
value by the tab-delimiter. This will result in an array of
arrays.
--->
<cfloop
index="rowIndex"
from="1"
to="#arrayLen( rows )#"
step="1">
<!---
Convert the row data to an array of field values (as
delimited by Tabs).
--->
<cfset rows[ rowIndex ] = reSplit( tab, rows[ rowIndex ] ) />
</cfloop>
<!--- Output our resultant CSV data. --->
<cfdump
var="#rows#"
label="Parsed CSV Data"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment