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:
This represents the characters in the set A that also exist in the set B.
Character class subtractions look like this:
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:
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.
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 also assume it was MX6.
Nice ... I sure wish ALL of Java's functionality was baked in to CF ...
What a rippin' system it would be then ...