ColdFusion Regular Expressions Do Not Support Character Class Intersection Or Subtraction

Posted February 6, 2009 at 3:56 PM by Ben Nadel

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

Feb 18, 2009 at 2:28 PM // reply »
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.


Feb 18, 2009 at 3:44 PM // reply »
11,244 Comments

@Brandon,

I also assume it was MX6.


Sep 5, 2011 at 1:14 PM // reply »
63 Comments

Nice ... I sure wish ALL of Java's functionality was baked in to CF ...

What a rippin' system it would be then ...


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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
May 23, 2013 at 5:19 AM
Ask Ben: Print Part Of A Web Page With jQuery
How to print also the background color of table cells and table lines ... read »
May 23, 2013 at 3:55 AM
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
very interesting and helpful too. ... read »
May 22, 2013 at 5:35 PM
Script Tags, jQuery, And Html(), Text() And Contents()
This is still an issue 2 years later. jQuery is supposed to remediate these cross browser issues, no? I have been unable to find any statement from the jQuery team calling this behavior "by de ... read »
May 22, 2013 at 12:44 PM
Ask Ben: Query Loop Inside CFScript Tags
In cf10, if you call a function that has: local.result = {}; local.result.msg = ""; local.svc = new query(); local.svc.setSQL("SELECT * FROM..."); local.obj = local.svc.exe ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools