User Guide

Table Of Contents
154 Chapter 7: Using Regular Expressions in Functions
Element one of the pos array contains the starting index in the search string of the string that
matched the regular expression. Element one of the
len array contains length of the matched
string. For this example, the index of the first " BIG " string is 5 and its length is also 5. If there
are no occurrences of the regular expression, the
pos and len arrays each contain one element
with a value of 0.
You can use the returned information with other string functions, such as
mid. The following
example returns that part of the search string matching the regular expression:
<cfset myString="Some BIG BIG string">
<cfset sLenPos=REFind(" BIG ", myString, 1, "True")>
<cfoutput>
#mid(myString, sLenPos.pos[1], sLenPos.len[1])#
</cfoutput>
Each additional element in the pos array contains the position of the first match of each
subexpression in the search string. Each additional element in
len contains the length of the
subexpressions match.
In the previous example, the regular expression " BIG " contained no subexpressions. Therefore,
each array in the structure returned by
REFind contains a single element.
After executing the previous example, you can call
REFind a second time to find the second
occurrence of the regular expression. This time, you use the information returned by the first call
to make the second:
<cfset newstart = sLenPos.pos[1] + sLenPos.len[1] - 1>
<!--- subtract 1 because you need to start at the first space --->
<cfset sLenPos2=REFind(" BIG ", "Some BIG BIG string", newstart, "True")>
<cfoutput>
<cfdump var="#sLenPos2#">
</cfoutput><br>
The following figure shows the output of the cfdump tag:
If you include subexpressions in your regular expression, each element of
pos and len after
element one contains the position and length of the first occurrence of each subexpression in the
search string.
In the following example, the expression [A-Za-z]+ is a subexpression of a regular expression. The
first match for the expression ([A-Za-z]+)[ ]+, is “is is”.
<cfset sLenPos=REFind("([A-Za-z]+)[ ]+\1",
"There is is a cat in in the kitchen", 1, "True")>
<cfoutput>
<cfdump var="#sLenPos#">
</cfoutput><br>