User Guide

Table Of Contents
Returning matched subexpressions 153
This example returns "Hello Mr. Bond". If you did not prohibit the capturing of the Hi/Hello
group, the \2 backreference would end up referring to that group instead of " Bond", and the
result would be "Hello Mr.ello".
Returning matched subexpressions
The REFind and REFindNoCase functions return the location in the search string of the first
match of the regular expression. Even though the search string in the next example contains two
matches of the regular expression, the function only returns the index of the first:
<cfset IndexOfOccurrence=REFind(" BIG ", "Some BIG BIG string")>
<!--- The value of IndexOfOccurrence is 5 --->
To find all instances of the regular expression, you must call the REFind and REFindNoCase
functions multiple times.
Both the
REFind and REFindNoCase functions take an optional third parameter that specifies the
starting index in the search string for the search. By default, the starting location is index 1, the
beginning of the string.
To find the second instance of the regular expression in this example, you call
REFind with a
starting index of 8:
<cfset IndexOfOccurrence=REFind(" BIG ", "Some BIG BIG string", 8)>
<!--- The value of IndexOfOccurrence is 9 --->
In this case, the function returns an index of 9, the starting index of the second string " BIG ".
To find the second occurrence of the string, you must know that the first string occurred at index
5 and that the string’s length was 5. However,
REFind only returns starting index of the string,
not its length. So, you either must know the length of the matched string to call
REFind the
second time, or you must use subexpressions in the regular expression.
The
REFind and REFindNoCase functions let you get information about matched subexpressions.
If you set these functions’ fourth parameter,
ReturnSubExpression, to True, the functions
return a CFML structure with two arrays,
pos and len, containing the positions and lengths of
text strings that match the subexpressions of a regular expression, as the following example shows:
<cfset sLenPos=REFind(" BIG ", "Some BIG BIG string", 1, "True")>
<cfoutput>
<cfdump var="#sLenPos#">
</cfoutput><br>
The following figure shows the output of the cfdump tag: