ColdFusion REFind() Cannot Find The Empty String

Posted December 16, 2008 at 8:56 AM by Ben Nadel

Tags: ColdFusion

When you step back and think about it, this makes perfect sense; but, yesterday, for a few minutes, I was completely stumped as to why my ColdFusion REFind() method call was not working. I was using regular expressions to validate strings and for strings that could be zero or more characters, I was using a regular expression that looked like this:

^.{0,50}$

It worked for strings that had positive lengths, but failed for zero length strings:

  • <!--- Create the value to match with the regular expression. --->
  • <cfset strValue = "" />
  •  
  • <!---
  • Create a regular expression that matches ZERO or more
  • characters in a complete string.
  • --->
  • <cfset strRegEx = "^.*$" />
  •  
  • <!---
  • Check to see if the regular expression pattern was
  • found in our empty string.
  • --->
  • <cfif REFind( strRegEx, strValue )>
  •  
  • Found!
  •  
  • <cfelse>
  •  
  • Not Found!
  •  
  • </cfif>

Running the above code gives us the following output:

Not Found!

If you run this in any kind of third-party regular expression tester, like The RegEx Coach, it will work fine. The problem is not in the regular expression. In fact, there really is no problem at all. The reason this doesn't work is that it cannot co-exist with the one-based indexing used in ColdFusion. The REFind() and REFindNoCase() functions return "the position in the string where the match begins;" since ColdFusion strings begin at index one, it is impossible to return the a match that has no length as it will also have no index. In other words, for the REFind() method to match the empty string it would have to also confirm that the target string has a length, which would be a contradiction.

In order to get this to work, I had to switch over to using the Java Pattern object's Matches() method:

  • <!--- Create the value to match with the regular expression. --->
  • <cfset strValue = "" />
  •  
  • <!---
  • Create a regular expression that matches ZERO or more
  • characters in a complete string.
  • --->
  • <cfset strRegEx = "^.*$" />
  •  
  • <!---
  • Check to see if the regular expression pattern was found in
  • our empty string. To do this, we are going to use the Java
  • Pattern object. Not only is Java is used to using zero-based
  • indexing, but this method returns a BOOLEAN rather than a
  • match offset.
  • --->
  • <cfif CreateObject( "java", "java.util.regex.Pattern" ).Matches(
  • JavaCast( "string", strRegEx ),
  • JavaCast( "string", strValue )
  • )>
  •  
  • Found!
  •  
  • <cfelse>
  •  
  • Not Found!
  •  
  • </cfif>

This time, when we run the code above we get the following output:

Found!

As you can see, the Java Pattern object was able to match the empty string.

Now, just to make sure that I was really on the right track, I tested to see if the REReplace() method, which doesn't care about string indexes, would match the empty string:

  • <!--- Create the value to match with the regular expression. --->
  • <cfset strValue = "" />
  •  
  • <!---
  • Create a regular expression that matches ZERO or more
  • characters in a complete string.
  • --->
  • <cfset strRegEx = "^.*$" />
  •  
  • <!---
  • Replace the regular expression pattern match with
  • a new value.
  • --->
  • <cfset strValue = REReplace(
  • strValue,
  • strRegEx,
  • "Replaced!",
  • "one"
  • ) />
  •  
  • <!--- Output our new string. --->
  • #strValue#

When we run this code, we get the following output:

Replaced!

That works just fine. So, like I said, when you step back and think about what the ColdFusion REFind() and REFindNoCase() methods actually do, it becomes obvious that they cannot match the empty string. But, when you have your nose deep in code, sometimes it's not that obvious.

And, as one more test, I just wanted to see if ColdFusion 8's new REMatch() function could match the empty string:

  • <!--- Create the value to match with the regular expression. --->
  • <cfset strValue = "" />
  •  
  • <!---
  • Create a regular expression that matches ZERO or more
  • characters in a complete string.
  • --->
  • <cfset strRegEx = "^.*$" />
  •  
  • <!--- Get all the matches for our empty string. --->
  • <cfset arrMatches = REMatch( strRegEx, strValue ) />
  •  
  • <!--- Output the first value. --->
  • Index 1: [#arrMatches[ 1 ]#]

When we run this, we get the following output:

Index 1: []

As you can see, ColdFusion's REMatch() does successfully match and return the empty string.



Reader Comments

Dec 16, 2008 at 2:33 PM // reply »
38 Comments

Ben,

You're too good looking to be talking about such nerdy topics. You should be wearing glasses with tape on them, a button down shirt with a pocket protector, etc. :)

But seriously, this is just more fuel for the "CF should have 0-based indexing" fire. It has always bothered me...


Dec 16, 2008 at 3:07 PM // reply »
10,640 Comments

@Jake,

Ha ha :) Thanks. Yeah, 0 vs. 1 is an interesting topic. I do like that having many things 1-based makes some boolean logic much more easy to write. For example:

<cfif ListFind()>

... is (to me) much more readable and intent-meaningful than:

<cfif (ListFind() GTE 0)>

But, that might be a small trade-off when considering a whole bunch of other things including cross-language compatibility.


Dec 16, 2008 at 6:14 PM // reply »
38 Comments

Yeah, good point, boolean logic is easier with 1-based indexing.


Dec 17, 2008 at 9:50 AM // reply »
7 Comments

This is just one of the many reasons why I prefer Pattern matching to the built in Regex functions. The expressions that Pattern recognizes are broader and more accurate then the built in functions.


Dec 17, 2008 at 9:54 AM // reply »
10,640 Comments

@Jon,

The Pattern matching / matcher is also much faster and provides access to individual captured groups. It's truly a wonderful library to have at our disposal.


Dec 17, 2008 at 3:09 PM // reply »
7 Comments

@Ben

Yep, that's why I worked through a list of Pattern based utility functions over on my site :)


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »