Finding Strings

When you need to know the position of an expression being searched within a string, you should use the REFind() or REFindNoCase() functions. These regular expression functions return the position and length of the first occurrence of the matched substring or pattern from the specified start position.

The difference between the two is that the REFind() function is case-sensitive and the REFindNoCase() function is not case-sensitive. These two functions share the same syntax, so you don't have to worry about learning any additional attributes from one to the other.

 REFind(reg_expression, string [, Start ] [, returnsubexpressions ])  REFindNoCase(reg_expression, string [, Start ] [, returnsubexpressions ]) 

As mentioned, you can find an exact string within another string, or you can search for a pattern if you do not know the exact string. This can come in handy when you're looking for things like email addresses within a larger string. We'll take a look at some code that works for finding email addresses in just a minute. First, however, let's look at an example of how to find a specific string within a larger string. Let's start by creating our string:

 <cfset string = "Now is the time for all good men"> 

Now let's search for a string within it:

 <cfoutput>          #REFind("time", string, "1")#  </cfoutput> 

The value that results from the preceding code is "12" because the expression "time" occurs at the twelfth character position in the string.

I promised that we'd look at an example of how to find an email address within a string. So, let's consider the following example:

 <cfset string = "Email me at"> 

Now for our regular expression:

 <cfoutput>        #REFind("[[:alnum:]\.\_]+@[[:alnum:].]+[[:alpha:]]", string, "1")#  </cfoutput> 

The result of our output here is "13", identifying again the position where the match begins. There is a limitation to the code above, however. Have you realized what it is? Well, it doesn't take into account the format of international email addresses. For better coverage, try something like this:

 <cfset string = "Email me at"> 

Now for our regular expression:

 <cfoutput>  #REFindNoCase("[[:alnum:]]  \.\_+([[:alnum:]])+@[[:alnum:]]+([[:alnum:]]+)*\.(([[:alpha:]]{2,3})|(aero|coop|info|m  useum|name))")#  </cfoutput> 

Now look at Listing 10.1. It enables you to capture email addresses within a string and print them out:

Listing 10.1 Find Multiple Email Addresses in a String
 <html>  <head>       <title>Regular Expressions</title>  </head>  <body>  <table>       <tr>            <td >  <br>  <br>  <cfset searchstring = "Please email, or with your questions.">  <cfset regex = "[[:alnum:]]+\@+([[:alnum:]]|[[:alnum:]]*\- [[:alnum:]])+(\.[[:alnum:]]+)*\.(([[:alpha:]]{2,3}))">  Our original search string: <br>  <cfoutput>#searchstring#</cfoutput>  <br>  <br>       <cfset startpos = 1>       <cfloop condition="startpos gt 0">             <cfset match = REFindNoCase(regex, searchstring, startpos, TRUE)>             <cfset foundvalues = Mid(searchstring, match.pos[1], match.len[1])>                  <cfoutput>                       #foundvalues#<br>                  </cfoutput>             <cfif match.len[1] is 0><cfbreak></cfif>             <cfset startpos=match.pos[1]+match.len[1]>       </cfloop>            </td>       </tr>  </table>  </body>  </html>  

Notice in Figure 10.1 that we have defined the regular expression outside the actual REFindNoCase() function call. We can do this with any regular expression and can even include those variables into our application to reuse the call in several places. Of course, there's a better way to handle this, but that's a discussion about user-defined functions.

Figure 10.1. Generated output from Listing 10.1.


The resulting output shows that each email address in the string has been found and written to the page. That's a bit more complex, no doubt, but it works quite well. We could also employ the code used in Figure 10.1 along with an insert query to build an email mailing list. Want to know more about this one? Visit our site at

Inside ColdFusion MX
Inside Coldfusion MX
ISBN: 0735713049
EAN: 2147483647
Year: 2005
Pages: 579 © 2008-2017.
If you may any questions please contact us: