Thursday, April 10, 2008

Find Replace Regex (regular expressions) Form, VB.NET (.NET Framework)

My latest tool, Standalone Find/Replace in VB.NET, to be added on anywhere (including your and my existing projects!) It is a simple standalone find replace form which uses a reference to a object to search it. You will probably only find this useful if you, like I, have to accommodate higher end users doing text editing (usually REGEX stuff in my case.) It is also a good example of leveraging Regex better than many I have seen elsewhere so I thought I would share it. It could be easily ported to C# using SharpDevelop.

It currently accepts the reference to a listview or textbox (with the idea of leaving room for other items I may find useful in the future) and finds text normally or using REGEX matching (.NET regexs, Rock on.)

I have not implemented the replace in the listview because I myself display tables or objects using a listview, but the listview is just to give the user an idea of what is going on in the object behind the scenes and editing the listview will have no effect on the object (unless you wanted to write a listview to object backward conversion for everything you do). Therefore, if you want it to work on your own objects you would have a code that in on your own, and if you want it to replace in listviews you will have to do that too! (unless I suddenly find the need) I think the find option alone on a listview is great though. The "Include subitems" checkbox is listview specific to be able to search all columns or not, optionally.

I got most of the motivation for the GUI from EditPlus a great text editing program which also has a powerful regular expression find replace built in I highly suggest it.

The options:
  • Include Subitems - As I said above this checkbox makes it where you can search all columns of a listbox instead of just the first, though you can turn it off when you are only concerned about the first column (or text) of the listviewitems.
  • Use Regex - makes the top box search by regular expression and has the bottom box replace by regex one of the great options is you can capture in the top area and then put those back in with $1, $2, $3, etc.
    • For instance (This is a bit complex but most would probably get it):
      • Topbox: (ab)(c) Bottom box: $2$1 Text found: abc Changed to: cab
      • Topbox: (http://)[^\.]+(\.[^/]+/) Bottom Box: $1technage$2 TextFound: Changed to:
  • Search Up - Changes from searching in the downward direction to searching in the upward direction.
This makes heavy use of general string functions as well as System.Text.RegularExpressions.

It contains a great use of MatchCollection.Result() which handles the reinsertion of the captures.

I can do some more chatting and example giving but for now I want to throw it out there and see if anyone has questions or is interested.

Sorry for the crudeness of the textboxes below, but I do not have a real place to host the files except on this blog. And due to the fact these are very small I just put them down there in the text areas to be copied out. If that is too annoying I can add them in a pre.

The codes I have published below are a great example for why one should have a replacer like this because the carriage returns in normal code for VB.NET are resulting in <br />'s to be placed in the code. I assume its a blogger bug, but if anyone knows a great trick to fix it that would be great I looked at some of the FAQ and the groups discussion no one had a definitive way to fix the problem except for removing the carriage returns, but for proper coding format that is the dumbest idea ever. Do people actually use Textarea for something other than displaying code???

NOTE: So use the tool to fix the code once you have compiled it, have it replace the double tabs (\t\t) with a single newline (\n) in regex mode. That should make it pretty again.




No comments: