Most Recent Blog

King's View of the Battle

 Open GL model of a chess board.

Showing posts with label productivity. Show all posts
Showing posts with label productivity. Show all posts

June 1, 2012

Eclipse/NetBeans Multi Line Search and Replace Across Files

We all know System.out.println statements are not good for performance and clutter logs.  We should use a logger instead.  Loggers allow for the control of logging levels.  How do you replace these System.outs in a large project?  Here are the steps used recently:
  1. Come up with a good REGular EXspresion (REGEX), for matching your desired code.
  2. Come up with your replacement regex and capture user.
  3. Ensure that your code compiles and inspect your changes.
In this case we want to replace the following:
package com.ecokrypt...

public class ToManySystemOuts {

    System.out.println("Single Line System.out.println");


    System.out.println("Rare multiline " +
           " System.out.println ");

}
We need to find a REGEX that will match both of these cases.  There are several ways to do any REGEX, but a solution that is easy  to understand that will work is:

   (?s)System.out.println(\(([^;]+?)\));

We have to dissect what this is saying to understand a couple of subtleties:
  • (?s) - indicates that we search past line feeds.  Without this we will not match the second case because the line feed would cause the REGEX to not match at all.
  • System.out.println - matches the exact phrase
  • (\(([^;]+?)\)) - is very complicated, and will take time before you can read this.  A technique that typically works is to read it inside out.  Trying that out:
    • [^;]+ - look for anything that is NOT a ';' (semicolon).  the + is saying that you have to match at least 1, and there is no limit to how many characters you can match.
    • ([^;]+?) - Has a real subtlety to it, Regexes have an ability to be greedy, or lazy.  A greedy regex will match the largest region it can.  A lazy regex will match the smallest area it can.  It will take time to figure out when to use which, but the basic advice given here is ALWAYS try to use lazy.  Only use greedy if you really need it.  This REGEX is obviously lazy, how can you tell?  Think about it a bit.
      • the ? indicates that you are going to look for the next sequence imediately after.  
      • the (...) indicates you are going to "capture" whatever you find presumably for later use.
      • therefore the ( ) are not matched but are just part of the regex capture syntax.
      • So the answer is: if the ? is inside the capture group e.g. (...?) the matching is lazy; if the ? is outside of the capture group, the matching is greedy e.g. (...)?
    • \(([^;]+?)\)  - says, adds the \(...\) to the previous the \ is an escape character and says match a ( and a ), don't create another capture group.
    • (\(...\)) - says, now add another capture group containing everything inside the literals ( ).
  • ; the semicolon at the end says match a literal semicolon.
That is pretty much all there is to it.  Look at the following REGEX, which might be a common mistake and see if you can figure out why it won't work.  Guess what it will match.  (HINT thing about greedy vs lazy).

   (?s)System.out.println(\(([^;]+)?\));

Now that we have our matching REGEX, we need to get our replacement REGEX:

   System.out.println$1;

This is much simpler, it says:  replace the above match, with logger.debug$1; where $1 is inclusive the literal (...).  e.g. the braces and everything inside of them.

How do we do this inside of Eclipse or NetBeans?
I find NetBeans to be much nicer in some respects, and not so nice in others.


NetBeans in File Search Replace
This is the netbeans view, it uses the space very nicely and there is NO POPUPS...  You really have to like the ergonomics of NetBeans.  The search is dynamic and nicely highlighted so you can see the search as you go.
NetBeans Replace Across All Files
USE CRTL-H for a shortcut to this Menu.

NetBeans Confirmation Dialog

Eclipse:

The Eclipse Search Dialog.  (Allows you to select scope)





May 11, 2011

Ecocrypt's RegEx Testing Applet and Java-Regex Source Generator

This is the Discussion Board For EcoCrypt's RegEx Testing Applet.

Feel free to use this, or bookmark the actual link http://www.ecocrypt.com/regex
*Features:

  • Test your regex pattern against text input.
  • Generate the Java source code for the regex matcher.
  • Run your regex against a file on your local harddrive.
Please Leave Comments! Comments Can Be Related To Wish List, Usability, Or Other RegEx Thoughts.



January 30, 2011

Using VI / VIM Tags for maintaining developer's notes

Have you ever wanted to maintain a set of developer notes, or other quick references documents, and you want to maintain private or sensitive information that should not be accessible from the internet, consider using vi or gvim with tags.

Purpose:
Maintain a set of notes related to current projects, current software systems, and other handy information that is needed quickly can be valuable.  Using vi/gvim to do this can improve the navigation of the notes files.  

Advantages:
  • VIM / GVIM are free easy to learn editors.
  • VIM / GVIM exist on almost all unix installs by default.
  • Use of 'tags' allows quick hyper-link style navigation though a file or set of files.
  • Very light weight and simple way of maintaining information.
What are tags?
Tags are bookmarks.  There are two sides to the tag:
  • The hyper-link or menu identifier.  Called the identifier tag.
  • The bookmarked location or tagged data.
within vim/gvim, one moves from a hyper-link or menu identifier to the tagged data or bookmark location by placing the cursor over the menu item, and pressing 'crtl-]'.

One can quickly jump back to the previous location (the hyper-link or menu identifier from which they came) by pressing the 'crtl-T'.

How do you create bookmarks, and bookmark locations?
  1. Creating a bookmark or tag:
    1. Use the |tagname| syntax to create a menu item or hyper-link in a text file.
    2. In the tags file associated with this text file create a single line entry as follows:
      tagname filename.txt /*tagname*
    3. The tags listed within the tag file will have to be sorted or the tag logic/hyper-link will not work.
  2. Tagging the location/setting the bookmark:
    1. within the text file, add a bookmark with the syntax *tagname*
    2. You are free to use the same tagname in multiple bookmarks, so for example if you wanted to use the tag with a name section at the beginning of each section, you could.
    3. You are also free to use multiple tags/bookmarks on a single line.  An example would be to include the following tags *section* *howtotags* *gvim_tags*.