O'Reilly logo

Programming ColdFusion by Rob Brooks-Bilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required