Current Filter:
Category: ColdFusion
Date Range: 6/1/2008 - 6/30/2008
(clear
filters)
Happy National Regex Day!
To celebrate I'd like to share some tips for working with Regular Expressions that have helped me over the years I've been using them. I've got 5 tips to share today (#1 is already in the post on Ben Nadel's blog that I linked to but thought it worthwhile to repeat here, sorry if it is a dupe for you...)
Tip #1, for Dreamweaver Users: If you're a Dreamweaver user and need to build a regular expression, start learning about the Server Behavior Builder. When you create a server behavior, Dreamweaver generates a regular expression to match the code block(s). If you're in a hurry (or especially if you're just learning regular expressions), the regexes Dreamweaver generates can be a good starting point. You'll find the regexes it generates are in your user's Configuration in .edml files (which are XML files).
Tip #2, for CFEclipse users (assuming this works in other flavors of Eclipse as well): When you use the Search dialog (ctrl+H on the Mac), there's the obvious Regular Expression checkbox. With your focus in the Containing Text field, press Ctrl+Space to get a handy regex cheat sheet.
Tip #3, a Workflow Tip: When trying to come up with that perfect regex, take the time to build a static page or two that has what you want to match in it. Essentially, create a use case document. Then use your favorite regex capable Find and Replace tool to work up to creating the perfect regex. For instance, if you have a huge string to match, start by trying to match only the first 5 characters then add on to your regex and test again. When you've got it matching your use cases, then go ahead and plug it into your ColdFusion or JavaScript code (you don't use other code right? ;-))
Tip #4, Debugging Regular Expressions: You'll most likely find yourself debugging a regex at some point or another, and for me its usually because I missed a use case. (That's when its time to update the use case document.) Any time I've got a regex that isn't quite working right, I trim it back. In other words, I do the opposite of what I described in the first part of my workflow tip above. Stop trying to match what you really want to match in its entirety, take a bit away from the regex at a time until it starts matching your use case file. (If you didn't make a use case file, now is the time to do it!) Then build that regex back up again little by little until you have it matching everything you want.
Tip #5, Application Comments: In the case of using a regex in an application (versus in Find and Replace), always, always make a comment on what that regex means in plain English terms and why you need to use it in the first place. Sooner or later, someone will look at the regex and quite possibly have their eyes glaze over -- it could be you! So do yourself and others a favor and take a couple minutes to comment it. If you're extra geeky you'll have your comment refer to your use case file so they can actually see exactly what the heck that fancy string of characters is supposed to match.
Posted by ~Angela | Comments (3) | Add Comment | Permalink
Comment from Sheran on 6/7/2008
Thanks for the information angela.
You're welcome. Glad to see a few people found this useful. ~Angela
Comment from Patrick McElhaney on 6/1/2008
Great tips. In fact, I think each one is progressively better!
Don't forget to enter in Steve Levithan's contest, if you haven't already.
Thanks Patrick. :-) I missed out on Steve's contest this year, but that's alright; I still enjoyed the new holiday. :-) ~Angela
Comment from Steve on 6/1/2008
Good stuff. You really should submit this here.
Bummer, I missed out... Oh well. Thanks for the suggestion anyhow. ~Angela