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-sensitive search; REFindNoCase( ) performs a case-insensitive 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)>
The Position:  #ThePosition#

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 its 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 MX, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.