Skip to main content
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: Scott Pinkston
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: Scott Pinkston

ColdFusion Regular Expressions Do Not Support Character Class Intersection Or Subtraction

By
Published in Comments (3)

When it comes to complex or long-running regular expressions, I almost always execute them in Java objects created in my ColdFusion code. Java regular expressions are much faster and more robust. I am always finding things that I can do in Java's version that I cannot do in ColdFusion's. Just today, I found another such feature: character class intersection and subtraction.

Character class intersections look like this:

[A&&[B]]

This represents the characters in the set A that also exist in the set B.

Character class subtractions look like this:

[A&&[^B]]

This represents the characters in the set A that do not also exist in the set B.

Ok, now let's take a look at a simple example. I am going to execute a replace using the same character class subtraction with ColdFusion's REReplace() and Java's ReplaceAll():

<!--- Set test string. --->
<cfset strData = "ABC1234567890XYZ" />

<!--- Replace all the numbers except for the evens. --->
#REReplace(
	strData,
	"[\d&&[^2468]]+",
	"_",
	"all"
	)#

<br />
<br />

<!--- Replace all the numbers except for the evens. --->
#strData.ReplaceAll(
	JavaCast( "string", "[\d&&[^2468]]+" ),
	JavaCast( "string", "_" )
	)#

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

ABC1234567890XYZ

ABC_2_4_6_8_XYZ

The regular expression character class union represents all the digits except for 2, 4, 6, and 8. As you can see, ColdFusion seems to completely ignore the character class altogether, making no replacement at all (subtraction or not). Java, on the other hand, successfully removes all of the odd-digit instances.

Every time I use Java's regular expression engine I am impressed with how fast and robust it is. If you haven't started playing around with it, I suggest you give it a try. Once you do, you're gonna find it a hugely useful tool.

Want to use code from this post? Check out the license.

Reader Comments

42 Comments

If it wasn't for being able to access the java, I am not sure what I would do sometimes. Java is blazing fast in concatenation and has a ton more to offer in array functionality.

Do you know what version of CF was the first to allow access to the underlying java? I though that it was 6, but I could be wrong.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel