The Dopefly Tech Blog

« The Dopefly Tech Blog Main page

Use the Java regular expression engine in ColdFusion

posted under category: ColdFusion on April 7, 2006 by Nathan

This is somewhat old news, but I've been using it lately, and figured I'd put a little reminder out here. Whenever you are trying to create a regular expression in ColdFusion and are having trouble with it, or if you're trying to use an advanced feature that just doesn't work (like lookbehind), remember that there IS a solution.

Here's a hint. Remember, CF is Java! Here's how to exploit the wonders of java.lang.String:

<cfset str = "This is my string!">
#str.matches("^.+$")#, displays YES
#str.replaceAll("\W","")#, displays Thisismystring
<cfdump var="#str.split("\W")#"/>, dumps an array of words

Everything you can do with a String can be found a the Java 1.4.2 API docs. (1.4.2 and not 1.5 because CFMX 6 & 7 support does not currently extend past 1.4.2)

Don't know enough about regular expressions? Learn more from the ColdFusion LiveDocs "Using Regular Expressions" pages, and also from regular-expressions.info.

Nathan is a software developer at The Boeing Company in Charleston, SC. He is essentially a big programming nerd. Really, you could say that makes him a nerd among nerds. Aside from making software for the web, he plays with tech toys and likes to think about programming's big picture while speaking at conferences and generally impressing people with massive nerdiness and straight-faced sarcastic humor. Nathan got his programming start writing batch files in DOS. It should go without saying, but these thought and opinions have nothing to do with Boeing in any way.
This blog is also available as an RSS 2.0 feed. Click your heels together and click here to contact Nathan.