Uses a regular expression (RE) to search a string for a pattern. The search is case sensitive.
For more information on regular expressions, including escape sequences, anchors, and modifiers, see "Using Regular Expressions in Functions" in the ColdFusion Developer's Guide.
Depends on the value of the returnsubexpressions parameter:
REFind(reg_expression
,string
[,start
,returnsubexpressions
] )
Find, FindNoCase, REFindNoCase, REReplace, REReplaceNoCase
Parameter |
Description |
---|---|
reg_expression |
Regular expression for which to search. Case-sensitive. |
string |
A string, or a variable that contains one, in which to search. |
start |
Optional. A positive integer, or a variable that contains one. Position in the string at which to start search. The default value is 1. |
returnsubexpressions |
Optional. Boolean. Whether to return substrings of reg_expression, in arrays named len and pos:
|
This function finds the first occurrence of a regular expression in a string. To find the second and subsequent instances of the expression or of subexpressions in it, you call this function more than once, each time with a different start position. To determine the next start position, use the returnsubexpressions parameter, and add the value returned in the first element of the length array to the value in the first element of the position array.
<h3>REFind Example</h3> <p>This example shows the use of the REFind function with and without the <i>returnsubexpressions</i> parameter set to True. If you do not use the <i>returnsubexpressions</i> 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.</p> <p>REFind("a+c+", "abcaaccdd"): <cfoutput>#REFind("a+c+", "abcaaccdd")#</cfoutput></p> <p>REFind("a+c*", "abcaaccdd"): <cfoutput>#REFind("a+c*", "abcaaccdd")#</cfoutput></p> <p>REFind("[[:upper:]]", "abcaacCDD"): <cfoutput>#REFind("[[:upper:]]", "abcaacCDD")#</cfoutput></p> <p>REFind("[\?&]rep = ", "report.cfm?rep = 1234&u = 5"): <cfoutput>#REFind("[\?&]rep = ", "report.cfm?rep = 1234&u = 5")# </cfoutput> </p> <!--- Set startPos to one; returnMatchedSubexpressions = TRUE ---> <hr size = "2" color = "#0000A0"> <p>If you use the <i>returnssubexpression</i> 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 <i>pos</i> and <i>len</i>, respectively.</p> <cfset teststring = "The cat in the hat hat came back!"> <p>The string in which the function is to search is: <cfoutput><b>#teststring#</b></cfoutput>.</p> <p>The first call to REFind to search this string is: <b>REFind("[A-Za-z]+",testString,1,"TRUE")</b></p> <p>This function returns a structure that contains two arrays: pos and len.</p> <p>To create this structure you can use a CFSET statement, for example: </p> <CFSET st = REFind("[[:alpha:]]",testString,1,"TRUE")> <cfset st = REFind("[[:alpha:]]",testString,1,"TRUE")> <p> <cfoutput> The number of elements in each array: #ArrayLen(st.pos)#. </cfoutput></p> <p><b>The number of elements in the pos and len arrays is always one if you do not use parentheses in the regular expression.</b></p> <p>The value of st.pos[1] is: <cfoutput>#st.pos[1]#.</cfoutput></p> <p>The value of st.len[1] is: <cfoutput>#st.len[1]#.</cfoutput></p> <p> <cfoutput> Substring is <b>[#Mid(testString,st.pos[1],st.len[1])#]</B> </cfoutput></p> <hr size = "2" color = "#0000A0"> <p>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.</p> <p>For example: <CFSET st1 = REFind("([[:alpha:]])[ ]+(\1)",testString,1,"TRUE")></p> <cfset st1 = REFind("([[:alpha:]]+)[ ]+(\1)",testString,1,"TRUE")> <p>The number of elements in each array is <cfoutput>#ArrayLen(st1.pos)# </cfoutput>.</p> <p>First whole expression match; position is <cfoutput>#st1.pos[1]#; length is #st1.len[1]#; whole expression match is <B>[#Mid(testString,st1.pos[1],st1.len[1])#]</B> </cfoutput></p> <p>Subsequent elements of the arrays provide the position and length of the first instance of each parenthesized subexpression therein.</p> <cfloop index = "i" from = "2" to = "#ArrayLen(st1.pos)#"> <p><cfoutput>Position is #st1.pos[i]#; Length is #st1.len[i]#; Substring is <B>[#Mid(testString,st1.pos[i],st1.len[i])#] </B></cfoutput></p> </cfloop><br>