View comments | RSS feed

REFind

Description

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 ColdFusion MX Developer's Guide.

Returns

Depends on the value of the returnsubexpressions parameter:

Category

String functions

Function syntax

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

See also

Find, FindNoCase, REFindNoCase, REReplace, REReplaceNoCase

Parameters

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:

  • 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 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.

Example

<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> &lt;CFSET st = REFind("[[:alpha:]]",testString,1,"TRUE")&gt; <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: &lt;CFSET st1 = REFind("([[:alpha:]])[ ]+(\1)",testString,1,"TRUE")&gt;</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>

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


frinky!!! said on Mar 1, 2005 at 6:00 AM :
Can anyone please explain why the results of the refind statements underneath are different, only bacause the '.' (dot) has moved to another place within the regexp?
is this a bug?

<cfset variables.testword = "abc123_-+%@">
<cfoutput>
REFind("[^a-z0-9 .-_]", variables.testword) =
#REFind("[^a-z0-9 .-_]", variables.testword)# (returns 8)
<br><br>
REFind("[^a-z0-9 -_.]", variables.testword) =
#REFind("[^a-z0-9 -_.]", variables.testword)# (returns 0?!?)
</cfoutput>
frinky!!! said on Mar 1, 2005 at 7:23 AM :
Ah, I found it out myself...
The answer to the question above: the '-' always creates a range between the character before the - and the character after the -.
I always thought that ranges within [ ] could only be created with a-z and numbers.
Maybe this should be documented somewhere?
a440guy said on Aug 8, 2005 at 12:58 PM :
It is documented, but by macromedia. The best book is "Mastering Regular Expressions" publiched by O'Reilly. Although the book covers regular expressions (RE) as found in Perl, ColdFusion's regular expressions are a subset of Perl's so it covers Coldfusion's as well. There are some minor differences in RE syntax (how and when to excape characters, for example, and the fact that CF's REs are strings, which means you have to do more escaping in CF), but all-in-all, this is the best book on the subject.

In your example, the [ ] is called a "character class". In a character class, all characters between the square brackets stand for themselves except ^ - ] \

If you want to match a circumflex, it must not be in the first position or it must be escaped. If you want to match a hyphen, right-square-bracket, or backslash you must escape them.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000606.htm