Finding Strings

To find a string in a block of text, you use the REFind( ) or REFindNoCase( ) functions. REFind( ) and REFindNoCase( ) are used when you want to know the position of the expression being searched for within a string. REFind( ) performs a case-insensitive search; REFindNoCase( ) performs a case-sensitive search. Regardless of the function used, they both use the same syntax:

REFind(RegularExpression, String [, StartPosition] [, ReturnSubExpressions])
REFindNoCase(RegularExpression, String [, StartPosition] [, ReturnSubExpressions])

Both the REFind( ) and REFindNoCase( ) functions accept the same parameters. The following example finds the first occurrence of “tom” regardless of case:

<CFSET MyString="This is a red tomato.">
<CFSET ThePosition= REFindNoCase("tom", MyString)>

<CFOUTPUT>
The Position:  #ThePosition#
</CFOUTPUT>

In this example, we use REFindNoCase( ) to find the starting position of “tom” within the string “This is a red tomato”. This is done by assigning the result of the regular expression to a variable—in this case, ThePosition. The actual regular expression is embedded within the REFindNoCase( ) function. In it’s basic form, REFindNoCase( ) accepts two parameters, the regular expression and the string. In this example, the regular expression is simply the three characters “tom”, and the string is “This is a red tomato”. Upon execution, the value returned for ThePositon should be 15 as the first occurrence of “tom” begins at position 15 of the string. ...

Get Programming ColdFusion now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.