CFLib.org – Common Function Library Project

reFindAll(regex, text)

Last updated July 15, 2005
Download UDF

author

Ben Forta                                         Ben Forta

Version: 1 | Requires: ColdFusion MX | Library: StrLib

 
Rated 2 time(s). Average Rating: 5.0

Description:
Returns all the matches of a regular expression within a string. This is simular to reGet(), but more closely matches the result set of reFind.

Return Values:
Returns a structure.

Example:

<cfset str = "his STR has some CAPS in it.">
<cfset caps = reFindAll("[A-Z]+",str)>
<cfdump var="#caps#" label="result">

Parameters:

Name Description Required
regex Regular expression. Yes
text String to search. Yes

Full UDF Source:

<!---
Returns all the matches of a regular expression within a string.

@param regex      Regular expression. (Required)
@param text      String to search. (Required)
@return Returns a structure.
@author Ben Forta (ben@forta.com)
@version 1, July 15, 2005
--->

<cffunction name="reFindAll" output="true" returnType="struct">
<cfargument name="regex" type="string" required="yes">
<cfargument name="text" type="string" required="yes">

<!--- Define local variables --->    
<cfset var results=structNew()>
<cfset var pos=1>
<cfset var subex="">
<cfset var done=false>
    
<!--- Initialize results structure --->
<cfset results.len=arraynew(1)>
<cfset results.pos=arraynew(1)>

<!--- Loop through text --->
<cfloop condition="not done">

<!--- Perform search --->
<cfset subex=reFind(arguments.regex, arguments.text, pos, true)>
<!--- Anything matched? --->
<cfif subex.len[1] is 0>
<!--- Nothing found, outta here --->
<cfset done=true>
<cfelse>
<!--- Got one, add to arrays --->
<cfset arrayappend(results.len, subex.len[1])>
<cfset arrayappend(results.pos, subex.pos[1])>
<!--- Reposition start point --->
<cfset pos=subex.pos[1]+subex.len[1]>
</cfif>
</cfloop>

<!--- If no matches, add 0 to both arrays --->
<cfif arraylen(results.len) is 0>
<cfset arrayappend(results.len, 0)>
<cfset arrayappend(results.pos, 0)>
</cfif>

<!--- and return results --->
<cfreturn results>
</cffunction>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
2 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson