November 5, 2022
Enabling CF to switch to using Java’s regex engine
Comments
(1)
November 5, 2022
Enabling CF to switch to using Java’s regex engine
ColdFusion troubleshooter
Guide 146 posts
Followers: 119 people
(1)

If you may ever encounter problems trying to use regular expressions in CFML (which are actually PERL regex’s), did you know that you can tell CF to use Java regex’s instead? This has been possible since 2019, but you could have missed when the change was introduced via CF2018 update 5 in Sep 2019–and of course the option is also built into CF 2021.

This is one of those settings which can be enabled/controlled at either:

  • the server level: via the CF Admin “Settings” page, and its “Use Java as Regex Engine” option
  • or the application level: via the this.useJavaAsRegexEngine in application.cfc (or an attribute of the same name in cfapplication, if using application.cfm)

Resources for more

For more on the feature, including examples, see the following.

First up, see of course the CF docs on the topic of Regex’s in CFML, which would be 17 pages if printed. It covers both forms and includes a table comparing them.

Second, Saurav Ghosh from Adobe had offered this this helpful post in Oct 2019 on the Adobe ColdFusion community forum. Note he shows examples using ReFind, ReFindNoCase, ReReplace, ReReplaceNoCase, ReMatch, and ReMatchNoCase. (The feature would affect any regex’s in any CFML.) He also offers a link to run his code on Adobe’s cffiddle.org site (note that you must login using a Google account to see such saved apps there).

Curiously, only his examples 15 and above really demonstrate results that DIFFER if one sets this useJavaAsRegexEngine. Somehow that blog-like post was never brought over here to the CF Portal, so I thought I’d do this today, when the subject came up with a client and I was showing the resources I mention here.

Ben Nadel also did a May 2020 blog post on the feature and offers some examples, while Adam Cameron used the feature to point out his distaste for such settings being settable as application settings, at the end of his 2022 blog post. Finally, for those who may wonder, Ben’s post sparked Lucee to add the feature, as of 5.3.8.79, in Jun 2021.

So again, this is not “new” information, but it may well be to you and other readers. Hope it’s helpful.


For more blog content from Charlie Arehart, see his posts here as well as his posts at carehart.org. And follow him on Twitter and other social media as carehart.

1 Comment
2022-11-11 17:27:09
2022-11-11 17:27:09

What happens if third party libraries aren’t expecting changes to the regex engine?  Would there be any problems? Is there any way to query the server or application to identify the regex engine currently being used?

Lazily written 3rd party libraries & component that have this flag enabled wouldn’t work in default environments.  They’d need to include disclaimers advising developers to explicitly add this flag to application-specific or global-server settings.

To avoid potential problems with 3rd party libraries, I believe it would be wiser to either specifically use the java syntax or a leverage a library like Ben Nadel’s JRegex library.
https://github.com/bennadel/ColdFusion-JRegEx

NOTE:  I’ve encountered (& reported) unresolved intermittent bugs w/CFReplaceNoCase. I don’t believe it’s been fixed and we’ve opted to use java’s replaceAll() & haven’t encountered any problems. (It seems to be faster in addition to being more memory efficient.)

javacast(“string”, myLargeString).replaceAll(“(?i)oldString”, “newString”);

Like
()
Add Comment