Skip to main content
Ben Nadel at InVision In Real Life (IRL) 2018 (Hollywood, CA) with: Vitaliy Mogilevskiy
Ben Nadel at InVision In Real Life (IRL) 2018 (Hollywood, CA) with: Vitaliy Mogilevskiy

PatternMatcher.cfc Now On GitHub

By
Published in Comments (4)

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.

Want to use code from this post? Check out the license.

Reader Comments

1 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.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel