Archive

Posts Tagged ‘Regular Expression’

Using Regular Expressions to build ColdFusion getters and setters

November 11th, 2008 Gareth 2 comments

I’m always looking for ways to make my life a little easier when writing code, and to take the monotony out of the equation wherever I can.  I know there is lots of back-and-forth about whether this method should or should not be used in ColdFusion, but I feel that for the minor incovenience of running a reg ex, creating getters and setters in my CFCs/Objects just make sense if just for the very fact of encapsulation.  Plus if you need to alter anything that occurs during a “set” operation, nothing outside of the object is broken when you make the change within your object.  Anyway, back to business:

This regular expression can be used in Eclipse (my current IDE) and will convert something that looks like:

<cfset variables.firstName = "" />
<cfset variables.lastName = "" />

into

<cffunction name="getfirstName" output="false" access="public" returntype="any">
<cfreturn variables.firstName />
</cffunction>
 
<cffunction name="setfirstName" output="false" access="public" returntype="void">
<cfargument name="val" required="true" />
<cfset variables.firstName = arguments.val />
</cffunction>

The search string is:

<cfset variables.(\w+) =  [^>]+>(\r\n)(\t)

and the replace string is:

<cffunction name="get$1" output="false"  access="public" returntype="any">$2$3$3<cfreturn variables.$1  />$2$3</cffunction>$2$2$3<cffunction name="set$1" output="false"  access="public" returntype="void">$2$3$3<cfargument name="val"  required="true" />$2$3$3<cfset variables.$1 = arguments.val  />$2$3</cffunction>$2$2$

The search string contains a “tab” character at the end. This is really just so I can format the output of the getters and setters, but feel free to modify them however you wish.

Regular Expressions in Flex Builder

April 15th, 2008 Gareth 2 comments

Up until a couple of months ago, I had not really used the regular expression Find/Replace in Eclipse. I’d read several blog posts about it, but hadn’t really played around with it much. This all changed after I was, once again, manually replacing some items in Eclipse (setting multiple properties of an object to those of an item that had been passed in to the function). About halfway through, I figured I would try using regular expressions to automate the task a bit. From this point on, just about every find/replace I did was using a regular expression.

All of these examples are for ActionScript, by the way, but could easily be converted to be useful in any other programming language:

For example: (Make sure to remove the double quotes from these examples also.)

I prefer curly braces in my functions to be on the same line as the actual function definition rather than on the next line so I wrote this

Find: "\s+\{"
Replace: " {"

This will take something like:

function testing():void
{

and change it to

function testing():void {

Not a huge deal to most, but my code OCD :) and our in-house coding standards like it on the same line. If I don’t have to manually do this, it certainly makes things easier.

Now this isn’t that complex and in most other IDE’s could be handled pretty easily. However, try doing something like this next example without using some kind of regular expression :)

(This example is pretty long, so make sure to remove the carriage return before the “return this” statement when pasting into your Replace field)

Find: "public var (\w+):( )?(\w+)( ?= ?[^;]+)?;(([^\t])+(\t)(\t+)?(\s+)?)"
Replace: "private var _$1:$3$4;$5$5public function get $1():$3 {$5$7
return this._$1;$5}$5$5 public function set $1( value:$3 ):void {$5$7this._$1 = value;$5}$5$5"

This example will take something like:

public var companyId:uint;
public var companyName:String = "";
public var isLegit:Boolean = false;

Will be converted to multiple accessors (i.e.setters and getters). I won’t show all 3, but this is what the first one would be converted to:

private var _companyId:uint;
 
public function get companyId():uint {
    return this._companyId;
}
 
public function set companyId( value:uint ):void {
    this._companyId = value;
}

That has saved me so much coding time.

Anyway, I wish I had started using these earlier as I’m now setting up a nice little library of scripts that I can run whenever I need to modify my code. I’m sure someone has set up a site with a library of these things, but these are just a few that I’ve set up.

And one very important thing to remember with these:

Make sure to check the Regular Expressions checkbox in Find/Replace or it will probably never find anything :)