REFindNoCase

Description

Uses a regular expression (RE) to search a string for a pattern, starting from a specified position. The search is case-insensitive.

For more information on regular expressions, including escape sequences, anchors, and modifiers, see Using Regular Expressions in Functions in the Developing ColdFusion Applications.

Returns

Depends on the value of the returnsubexpressionsparameter:

  • If returnsubexpressions= "False":

    • The position in the string where the match begins

    • 0, if the regular expression is not matched in the string

  • If returnsubexpressions = "True": a structure that contains two arrays, len and pos. The array elements are as follows:

    • If the regular expression is found in the string, the first element of the len and pos arrays contains the length and position, respectively, of the first match of the entire regular expression.

    • If the regular expression contains parentheses that group subexpressions, each subsequent array element contains the length and position, respectively, of the first occurrence of each group.

    • If the regular expression is not found in the string, the first element of the len and pos arrays contains 0.

Function syntax

REFindNoCase(reg_expression, string [, start, returnsubexpressions])

Parameters

Parameter

Description

reg_expression

Regular expression for which to search. Case-insensitive.

For more information, see Using Regular Expressions in Functions in the Developing ColdFusion Applications.

string

A string or a variable that contains one. String in which to search.

start

Optional. A positive integer or a variable that contains one. Position 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:

  • True: if the regular expression is found, the first array element contains the length and position, respectively, of the first match. If the regular expression contains parentheses that group subexpressions, each subsequent array element contains the length and position, respectively, of the first occurrence of each group. If the regular expression is not found, the arrays each contain one element with the value 0.

  • False: the function returns the position in the string where the match begins. Default.

Usage

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 returnsubexpressionsparameter, and add the value returned in the first element of the length array to the value in the first element of the position array.

Example

<h3>REFindNoCase Example</h3> 
<p>This example demonstrates the use of the REFindNoCase function with and  
    without the <i>returnsubexpressions</i> parameter set to True.</p>  
<p>If you do not use the <i>returnsubexpressions</i> parameter, REFindNoCase 
    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>REFindNoCase("a+c+", "abcaaccdd"):     
<cfoutput>#REFindNoCase("a+c+", "abcaaccdd")#</cfoutput></p> 
<p>REFindNoCase("a+c*", "abcaaccdd"):     
<cfoutput>#REFindNoCase("a+c*", "abcaaccdd")#</cfoutput></p> 
<p>REFindNoCase("[[:alpha:]]+", "abcaacCDD"):     
<cfoutput>#REFindNoCase("[[:alpha:]]+", "abcaacCDD")#</cfoutput></p> 
<p>REFindNoCase("[\?&]rep = ", "report.cfm?rep = 1234&u = 5"): 
<cfoutput>#REFindNoCase("[\?&]rep = ", "report.cfm?rep = 1234&u = 5")# 
    </cfoutput></p> 
<!--- Set startPos to one; returnMatchedSubexpressions = True ---> 
<hr size = "2" color = "#0000A0"> 
<p>If you do use the <i>returnssubexpression</i> parameter, REFindNoCase 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 REFindNoCase to search this string is:  
<b>REFindNoCase("[[:alpha:]]+",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> 
&lt;CFSET st = REFindNoCase("[[:alpha:]]+",testString,1,"True")&gt; 
<cfset st = REFindNoCase("[[: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 will always be one,  
    if you do not use parentheses to denote subexpressions 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 to denote subexpressions 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 subexpression within will be included  
    in additional array elements.</p> 
<p>For example: 
&lt;CFSET st1 = REFindNoCase("([[:alpha:]]+)[ ]+(\1)",testString,1,"True")&gt;</p> 
<cfset st1 = REFindNoCase("([[: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>