Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at the New York ColdFusion User Group (Mar. 2009) with:

ColdFusion REFind() Cannot Find The Empty String

By Ben Nadel on
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.


Looking For A New Job?

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

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

Reply to this Comment

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

Reply to this Comment

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.

Reply to this Comment

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

Reply to this Comment

Please any one me.. how can i compare 2 blog posts using coldfusion function or any method. i need to remove blog if two posts are 80% matching

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
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.