Follow Me On...

Entries in RegEx (2)

Wednesday
May202009

Using RegEx to comment out log statements cleanly.

I needed to optimize my code space usage on a recent embedded project. I wanted to estimate how much space was being used by my various print statements. In my project I have debug, info, warn, error level print statements.  I used the regex below to find each of the statements and  was able to find all statements, comment them out, and rebuild my project to see
how much space each log level used.

Find: (^\s*)((info)\S*\(.*)
Replace: $1;//CSOPT_$3 $2
File Types: *.c <—- important otherwise you could update header files or .txt files.

Example:
    infoln(“some info statement”);    —- BECOMES —->
    ;//CSOPT_info infoln(“some info statement”);

To use this regex you replace the “info” text with the command prefix you want to replace. It also will find things like infoln.  You may be wondering why the replace expression looks so complicated…

  • For one, you do not need to edit each replace statement for each find.
  • It maintains whitespace properly.
  • The reason for the ; in replace is to prevent issues where you had a if statements without brackets… the next statement isn’t what you meant for it to be.

To reenable your statements all you have to do is search for “//CSOPT_info ” (to replace one log type ata time) or “//CSOPT_\S+\s” (to replace all at once) and leave the replace box blank.

One issue you may encounter is if you have print statements which extend on to multiple lines… these you will either need to combine into one line or handle specially. Since these always create a compile error it’s not a big deal.

 

Tuesday
May202008

Eclipse RegEx Syntax & Capture Groups

Capture groups can be used to do the following:

Search Text: Blake Robertson
Find RegEx: (.*) (.*)
Replace RegEx: $2 $1
will change the text to: Robertson Blake
The $1 in the Replace RegEx matches to the first (.*) in the find expression and the $2 matches the second (.*)

 
More practical example… in my code i had a bunch of log statements like debug(“hello world\r\n”);  I was running low on program space in my PIC microprocessor and decided to replace them all with something like debugln(“hello world”); which saves us a whopping two bytes. 

If you want to replace a portion of line of code and you want to keep the actual print statement (the hello world part) you have to use a capture group.

Here’s the first regular expression i came up with:


FIND: debug\(\"(.*)\\r\\n"\);
REPLACE: debugln\(\"$1\"\);

This works and illustrates that whatever was originally in the (.*) will be inserted into the replacement string where the $1 is.

Here is the final FIND, REPLACE combo i came up with which would also work with a string which has additional printf style arguments and optional whitespace in various parts of the expression such as:
debug( “sum = %u\r\n”, x ) ;


FIND: debug\(\s*\"(.*)\\r\\n\"(.*)\)\s*;
REPLACE: debugln\(\"$1\"$2\);

Note: my replace expression eliminates any of the white space that was matched in the FIND expression.