How is using cfregex better than java.util.regex?

42 views
Skip to first unread message

Henry Ho

unread,
May 31, 2012, 8:01:29 PM5/31/12
to cfreg...@googlegroups.com
How is using cfregex better than java.util.regex using simple createObject("java","java.util.regex")?

Also, why does cfregex need "access to internal ColdFusion Java components"?

How much more is cfregex doing than simple wrapper to java.util.regex other than friendlier custom tag / CFC?

Thanks,
Henry

Peter Boughton

unread,
May 31, 2012, 9:01:54 PM5/31/12
to cfreg...@googlegroups.com
Hi Henry,


> How is using cfregex better than java.util.regex using simple createObject("java","java.util.regex")?

Well it's not necessarily better, so much as doing a lot of it for you - it saves needing to know what classes are involved, and provides certain common functionality.

In my own code, I probably use string.replaceAll( ... ) more than cfregex's replace, because I mostly don't need the extra features it brings, but for matching it's almost always more convenient to use cfregex. (It's rare that I'll need to do stuff with java.util.regex Matcher class that cfregex doesn't do for me.)


> Also, why does cfregex need "access to internal ColdFusion Java components"?

Actually, it doesn't.

The wording was intended to indicate it needs permission to do the above createObject call - some shared hosting disable that - and I copied the wording from the CF admin setting "Disable access to internal ColdFusion Java components" under the assumption that this was how createObject/type=java is disabled.

Looking more closely, this wording is talking about something else. I've just disabled this setting and tested it and it works fine.

I'll update the wording of this to correctly state what it needs.


> How much more is cfregex doing than simple wrapper to java.util.regex other than friendlier custom tag / CFC?

Well it provides code that you'd (eventually) write yourself if you were going to use java.util.regex, but also adds some additional functionality, and ensures all this is done in a consistent manner.

For example...
The `quote` function is a very thin wrapper for the java.util.regex version - there's really nothing extra it does.
The `find` function doesn't have a direct counterpart, so cfregex makes the relevant calls to the Matcher class to get its information.
The `replace` function supports a function to be passed as the replacement item - this is something JavaScript regex supports, but java.util.regex doesn't. Or you can pass in an array of replacement values which is cycled through - I think that idea came from Python or PHP, but again it's not native to java.util.regex so it's not simply wrapping a .replaceAll call for this.

So yeah, it's more than just a friendly wrapper, but how much more depends on how useful the extra functionality is to you - I've hopefully covered all the functionality in the documentation, but it generally compares differences vs CFML's refind/etc functions rather than what's added on top of the java.util.regex stuff.


Hope that answers your questions and all makes sense?


Thanks,

Peter

Henry Ho

unread,
May 31, 2012, 9:47:00 PM5/31/12
to cfreg...@googlegroups.com
Yes, everything makes sense.  Thank you for your time and effort.


Henry
Reply all
Reply to author
Forward
0 new messages