Skip to main content
Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.

PatternMatcher.cfc Now On GitHub

By Ben Nadel on
Tags: ColdFusion

A few years ago, I posted some code for a ColdFusion component that provided simple yet powerful methods for Regular-Expression-based string replacement. This morning, when I went to move it over to GitHub, I was surprised to find it completely absent from my list of projects. As such, I took the opportunity to rewrite the whole ColdFusion component in CFScript and post it on GitHub.

Project: View PatternMatcher.cfc on my GitHub account.

As a refresher, the PatternMatcher.cfc is a ColdFusion component that provides access to the underlying Java Regular Expression replacement libraries while removing all of the ceremony of type-casting and Java object creation. And, unlike functions such as reReplace(), PatternMatcher.cfc allows the user to replace pattern matches on an individual basis, rather than all at once.

Ultimately, however, the PatternMatcher.cfc ColdFusion component has three general modes:

  • Batch replacement.
  • Per-Match replacement.
  • Batch extraction.

To see the per-match replacement in action (since I think this is the most exciting part of the ColdFusion component), take a look at this example:


	savecontent variable="input" {
		writeOutput( "Sarah,212-555-0001," & chr( 10 ) );
		writeOutput( "Joanna,212-555-0002," & chr( 10 ) );
		writeOutput( "Kim,212-555-0003," & chr( 10 ) );
		writeOutput( "Libby,212-555-0004," & chr( 10 ) );

	// Find the email addresses so we can "*"-out the domain portion.
	// Notice that we have two capturing groups in the following
	// pattern:
	// 1 : the user + "@".
	// 2 : the domain.
	matcher = new lib.PatternMatcher( "(?<=,)([^@]+@)([^\n]+)", input );

	// Find each email individually.
	while ( matcher.findNextMatch() ) {

			"$1" &
			repeatString( "*", len( 2 ) ) )


	writeOutput( "<pre>#matcher.result()#</pre>" );


Here, we are looping over the matched email values and replacing a portion of the email address such that the domain-name is obscured. When we run the above code, we get the following page output:


Notice that while I matched the entire email address in the regular expression pattern, I was able to replace only a portion of it, on a case-by-case basis.

Anyway, Regular Expressions are super amazing. And, the Java pattern matching libraries are super powerful. So, hopefully this library brings them together in a very easy-to-consume manner.

Tweet This Deep thoughts by @BenNadel - PatternMatcher.cfc Now On GitHub Woot woot — you rock the party that rocks the body!

Reader Comments

Hey there I am so grateful I found your weblog, I really found you by accident, while I was browsing on Google for something else, Anyhow I am here now and would just like to say many thanks for a marvelous post and a all round exciting blog (I also love the theme/design), I don?t have time to go through it all at the minute but I have saved it and also added your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the great work.

Sorry, comments have been temporarily disabled due to spam. I'm busy implementing a new moderation feature because... some people are terrible!