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 cf.Objective() 2010 (Minneapolis, MN) with:

ColdFusion Regular Expressions Do Not Support Character Class Intersection Or Subtraction

By Ben Nadel on
Tags: ColdFusion

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.



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