Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with:

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:

  • <cfscript>
  •  
  • savecontent variable="input" {
  • writeOutput( "Sarah,212-555-0001,sarah@bennadel.com" & chr( 10 ) );
  • writeOutput( "Joanna,212-555-0002,joanna@bennadel.com" & chr( 10 ) );
  • writeOutput( "Kim,212-555-0003,kim@bennadel.com" & chr( 10 ) );
  • writeOutput( "Libby,212-555-0004,libby@bennadel.com" & 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() ) {
  •  
  • matcher.replaceMatch(
  • "$1" &
  • repeatString( "*", len( matcher.group( 2 ) ) )
  • );
  •  
  • }
  •  
  • writeOutput( "<pre>#matcher.result()#</pre>" );
  •  
  • </cfscript>

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:

Sarah,212-555-0001,sarah@************
Joanna,212-555-0002,joanna@************
Kim,212-555-0003,kim@************
Libby,212-555-0004,libby@************

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 Titillating read by @BenNadel - PatternMatcher.cfc Now On GitHub Thanks my man — 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.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.