jreget_function.cfm.txt ( 2,529 Bytes )
This uses a Java regular expression to get the matching groups found in the given string. Java's regular expressions are much faster and more powerful than ColdFusions and so this method can be much more useful than any buit in ColdFusion tag; however, realize that under the hood, this method is creating several Java object and parsing out a string, so there will be some overhead associated with it.
Please view the method definition below or download the demonstration code.
<cffunction name="JREGet" access="public" returntype="array" output="no"
hint="This takes a string and returns an array of all the matches using the Java RegExp">
<!--- Define arguments. --->
<cfargument name="Text" type="string" required="true" />
<cfargument name="RegExp" type="string" required="true" />
<cfscript>
// Define the local scope.
var LOCAL = StructNew();
// Create the java pattern complied to the given regular expression.
LOCAL.Pattern = CreateObject("java", "java.util.regex.Pattern").Compile( ARGUMENTS.RegExp );
// Create the java matcher based on the given text using the
// compiled regular expression.
LOCAL.Matcher = LOCAL.Pattern.Matcher( ARGUMENTS.Text );
// Create an array to capture the matches.
LOCAL.Matches = ArrayNew( 1 );
// Loop over the matcher while we still have matches.
while ( LOCAL.Matcher.Find() ){
// Get the current match.
ArrayAppend( LOCAL.Matches, LOCAL.Matcher.Group() );
}
// Return the results array which may or may not have any matches in it.
return( LOCAL.Matches);
</cfscript>
</cffunction>
Added April 20, 2006 / Updated April 20, 2006