New CFC: Regex
So I've been playing around with SES urls recently (for the urls of this blog), which lead to a newly found love for regular expressions. Unfortunately I was looking for something a little more robust that Coldfusion's ReFind method.ᅠ I wanted something that would return the actual matched string (not just the position and length).
This seemed easy enough, I need to get the code section of entries finished so I can actually show an example, but I simply wrapped ReFind in a nice method that stripped out the match.ᅠ So then I got a little fancier and decided it would be cool to return an array of matches, so I just did a nice loop over the results, stripped out each match and put them in an array... easy.ᅠ At this point I had what I needed for the blog, but I still wanted more.ᅠ Now I wanted to something that would return back references.
This lead me to CFLIB where I tried out Aaron Eisenberger's preg_match and preg_match_all UDFs.ᅠ These happen to be very similar to PHP's methods (hence the names) and they returned an array of matches with back references.
After using these methods for a while I stumbled across some of Ben Nadel's posts on regular expressions.ᅠ This post lead me to Steve Levithan's Flagrant Badassery post about the ReMatch UDF he created.ᅠ Steve's UDF seemed perfect, with just a single catch.ᅠ I was now back to no back references.ᅠ After playing with Steve's UDF, I decided to add a small change.ᅠ The scope argument now accepts 'ONE', 'ALL' and 'SUB'.ᅠ SUB will return back references (similar to the preg_match_all method).ᅠ So with that I know have a CFC that seems to do about everything I know how to do with regular expressions (at this point).ᅠ I'm still not up to speed with the things mentioned in Steve's post, but I hope to be someday.
So I'd like to give a big shout out to Aaron and Steve.ᅠ Most of what was done here was pieced together and inspired by them.ᅠ I do not take credit for most of what was done in this CFC.ᅠ I simply put together something that works well for me.ᅠ You can get a copy here: Regex CFC
Thanks again Aaron and Steve!
Andrew,
That is a useful modification. Thanks for sharing!