Orange is my favorite color

When I posted the regexp for adding output=”false” recently, it reminded me that I had saved a number of other regular expressions for previous search-and-replace operations. Here’s three others I’ve found helpful in cleaning up code in the past.

Strip whitespace from end of lines

Extra white space at the end of a line probably won’t cause any problems but it’s unnecessary and just taking up space. You can strip it with this.

Find: ([\S])[ |\t]+$
Replace: $1

A variant of this one is to find “^[ |\t]+$” which will find all lines that only contain spaces and tabs.

Find unescaped ampersands

Most ampersand characters need to be escaped in web apps – this will track all ampersands not followed by the most commonly escaped variants like   – add your own as needed.

Find: [^ ]&[^ |(amp;)|(nbsp;)|(gt;)|(lt;)]

 

Convert isDefined to structKeyExists

StructKeyExists is the preferred method of checking if a variable is defined but updating all of those isDefined()s by hand is a drag. Convert your entire code base with this search and replace:

Find: [iI]sDefined\("(FORM|URL).([A-Za-z0-9]+)"\)
Replace: structKeyExists($1, "$2")

3 Comments

  1. Dan G. Switzer, II said:

    on February 9, 2011 at 5:58 am

    RE: Strip whitespace from end of lines, most IDE provide a method for doing this automatically on saving a file.

    CFEclipse offers this option under Window > Preferences > CFEclipse > Editor > Trim trailing spaces before saving

    I like my IDE just handling this for me, because it’s one less thing I have to manage manually!

  2. Dan G. Switzer, II said:

    on February 9, 2011 at 6:01 am

    Oh yeah, another useful RegEx is one I worked up for finding variables w/in a block that don’t appear to be inside a block:

    http://blog.pengoworks.com/index.cfm/2008/7/23/Using-Eclipse-to-find-queries-that-arent-using-cfqueryparam-

    ]*>([^#]*(((?<!value=")#[^#]*#)))((?<!]*?)

    The regex does not explicitly check for the token <cfqueryparam, but instead checks to make sure that CF variables are preceded with the string value="—which is the attribute used in .

    The regex isn’t perfect and may pick up occasional false positives, but from my testing it seems to work pretty well.

  3. Brian said:

    on February 10, 2011 at 11:51 pm

    Great ones Dan – thanks! I agree about the IDE solving the whitespace issue but a lot of times you need to process an entire folder or project so it’s helpful to have it clean it up.

{ RSS feed for comments on this post}