| struct | |||||
|---|---|---|---|---|---|
| LEN |
| ||||
| POS |
| ||||
REFind Example
This example shows the use of the REFind function with and without the returnsubexpressions parameter set to True. If you do not use the returnsubexpressions parameter, REFind returns the position of the first occurrence of a regular expression in a string starting from the specified position. Returns 0 if no occurrences are found.
REFind("a+c+", "abcaaccdd"): 4
REFind("a+c*", "abcaaccdd"): 1
REFind("[[:upper:]]", "abcaacCDD"): 7
REFind("[\?&]rep = ", "report.cfm?rep = 1234&u = 5"): 11
If you use the returnssubexpression parameter, REFind returns the position and length of the first occurrence of a regular expression in a string starting from the specified position. The position and length variables are stored in a structure. To access position and length information, use the keys pos and len, respectively.
The string in which the function is to search is: The cat in the hat hat came back!.
The first call to REFind to search this string is: REFind("[A-Za-z]+",testString,1,"TRUE")
This function returns a structure that contains two arrays: pos and len.
To create this structure you can use a CFSET statement, for example:
<CFSET st = REFind("[[:alpha:]]",testString,1,"TRUE")>The number of elements in each array: 1.
The number of elements in the pos and len arrays is always one if you do not use parentheses in the regular expression.
The value of st.pos[1] is: 1.
The value of st.len[1] is: 1.
Substring is [T]
However, if you use parentheses in the regular expression, the first element contains the position and length of the first instance of the whole expression. The position and length of the first instance of each parenthesized subexpression within is included in additional array elements.
For example: <CFSET st1 = REFind("([[:alpha:]])[ ]+(\1)",testString,1,"TRUE")>
| struct | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| LEN |
| ||||||||
| POS |
| ||||||||
The number of elements in each array is 3 .
First whole expression match; position is 16; length is 7; whole expression match is [hat hat]
Subsequent elements of the arrays provide the position and length of the first instance of each parenthesized subexpression therein.
Position is 16; Length is 3; Substring is [hat]
Position is 20; Length is 3; Substring is [hat]
