Jay Taylor | programmer notes

Aug/10

11

Fun with Regular Expressions: Scrubbing the Hungarian

Found myself sort of longing to un-hungarianize some PHP classes.  Here is more information regarding Hungarian Notation.

First off, vim rocks.

Here is the regex to (pretty safely and reliably) de-hungarianify a PHP class:

(Each group is colored for clarity in identifying which section is which in the explanation)

%s/$[a-z]([A-Z])([a-zA-Z]+)/$l12/ge | %s/(private|protected|public|var)s+($_?)[a-z]([A-Z][a-zA-Z]+)/1 2l3/ge | %s/($this->_?)[a-z]([A-Z][a-zA-Z]+)([^(])/1l23/ge

Explanation:

%s/$[a-z]([A-Z])([a-zA-Z]+)/$l12/ge

Does simple variable name transformation of $bSomeFoo into $someFoo. This also removes the hungarian notation from statically referenced variables.

%s/(private|protected|public|var)s+($_?)[a-z]([A-Z][a-zA-Z]+)/1 2l3/ge

Strips the hungarian notation from class member variable declaractions, e.g. private $_bSomeOtherFoo into private $_someOtherFoo.

%s/($this->_?)[a-z]([A-Z][a-zA-Z]+)([^(])/1l23/ge

Strips the hungarian notation from class member variable references, e.g. $this->_bSomeOtherFoo into $this->_someOtherFoo.

Keep in mind that this could still break stuff if public class member variables are referenced externally in another file, or if the file you run this on references hungarian-notation in other classes that you haven’t updated.

RSS Feed

No comments yet.

Leave a comment!

You must be logged in to post a comment.

<<

>>

Find it!