Removing line numbers from a source code

At the moment I’m learning developping eclipse plugins for my work. Therefore I had to copy some code 1 from this tutorial. Unfortunately I had to remove the stupid line numbers. It’s best to let remove them by a script. Because I spent a lot of time in WordPress plugin development few weeks ago I chose PHP to do the work.

<?php
$code = '
1.   package com.myplugin.rmp.views;
2.   import java.util.ArrayList;
3.   import org.eclipse.core.resources.IFile;
4.   import org.eclipse.core.resources.IFolder;
5.   import org.eclipse.core.resources.IProject;
6.   import org.eclipse.core.resources.IResource;
7.   import org.eclipse.core.resources.IWorkspace;
8.   import org.eclipse.core.resources.ResourcesPlugin;
9.   import org.eclipse.core.runtime.IAdaptable;
10.  import org.eclipse.jface.action.Action;
11.  import org.eclipse.jface.action.MenuManager;
12.  import org.eclipse.jface.dialogs.MessageDialog;

...

199.         public void setFocus() {
200.                  viewer.getControl().setFocus();
201.         }
202. }
';

echo "<pre>". preg_replace("/\d+\./", "", $code) ."</pre>";
?>

The script is self descriptive but be sure that you adopt the regular expression to your needs. The regex I used destroys code containing constructs like anyObject12.doAnything();

  1. Copy & paste is bad but 200 lines are too many to rewrite ;-)
DeliciousDiggTechnorati FavoritesRedditLinkedInFacebookSpurlTwitterWebnewsYiGGMySpaceYahoo BookmarksFriendFeedGoogle BookmarksLiveJournalShare

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>