<cffunction name="REMatchGroup" access="public" returntype="array" output="false" hint="Gets the given group of a captured expression."> <!--- Define arguments. ---> <cfargument name="Pattern" type="string" required="true" hint="The regular expression we want to match on." /> <cfargument name="Text" type="string" required="true" hint="The target text against which we will run the patterns." /> <cfargument name="Group" type="numeric" required="false" default="0" hint="The captured group we want to return." /> <!--- Define the local scope. ---> <cfset var LOCAL = {} /> <!--- Create the results array. Here is where we will store our captured groups. ---> <cfset LOCAL.Results = [] /> <!--- Compile our regular expression pattern. By using the underlying Java pattern class, we will have both faster pattern matching and more access to the matched groups. ---> <cfset LOCAL.Pattern = CreateObject( "java", "java.util.regex.Pattern" ).Compile( JavaCast( "string", ARGUMENTS.Pattern ) ) /> <!--- Get our pattern matcher for our target text. ---> <cfset LOCAL.Matcher = LOCAL.Pattern.Matcher( JavaCast( "string", ARGUMENTS.Text ) ) /> <!--- Keep looping over the pattern matches until we hit the end of the target string. ---> <cfloop condition="LOCAL.Matcher.Find()"> <!--- Append the target group to the results array. If the Group was not defined (defaulting to zero), then the entire pattern will be matched. ---> <cfset ArrayAppend( LOCAL.Results, LOCAL.Matcher.Group( JavaCast( "int", ARGUMENTS.Group ) ) ) /> </cfloop> <!--- Return the results array. ---> <cfreturn LOCAL.Results /> </cffunction>