ColdFusion REFind() Cannot Find The Empty String
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.
Want to use code from this post? Check out the license.
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...
@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.
Yeah, good point, boolean logic is easier with 1-based indexing.
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.
@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.
@Ben
Yep, that's why I worked through a list of Pattern based utility functions over on my site :)
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