Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Tuesday, April 15, 2008

D'oh! - My Overcomplication of Regex


Some more screwing around with regex more has made me realize I have overcomplicated capture replacement, it is a simple as using a replace and just throwing in the $1, etc into the Regex.Replace functions replace string. I mean what I do works but it is totally unnecessary so I thought I would throw that out there. Apparently they already though of that when they did replace, I wonder why they didnt think of the tristate check treeview since a checkbox can be tristate. Sometimes it is better not to ask why!

The way I did it works well enough but you can see how it could be done below without a temp variable, and it is a little less confusing than using Regex.Result.


Anyway I thought I should share my ignorance, and enlightenment.

ElseIf cbRegEx.Checked = True AndAlso System.Text.RegularExpressions.Regex.IsMatch(txtfind.Text.ToLower, cmbFind.Text) Then
If System.Text.RegularExpressions.Regex.IsMatch(txtfind.Text.Substring(txtfind.SelectionStart, txtfind.SelectionLength), cmbFind.Text) Then

'Smarter, less lines System.Text.RegularExpressions.Regex.Replace(txtfind.Text.Substring(txtfind.SelectionStart, txtfind.SelectionLength), cmbFind.Text, cmbReplace.Text)

'Dumber Many more lines, except the highlighting which is needed
Dim mc As System.Text.RegularExpressions.Match


mc = System.Text.RegularExpressions.Regex.Match(txtfind.Text.Substring(txtfind.SelectionStart, txtfind.SelectionLength), cmbFind.Text)


temprep = cmbReplace.Text


temprep = mc.Result(cmbReplace.Text)

System.Text.RegularExpressions.Regex.

Dim tempstart As Integer = txtfind.SelectionStart

Dim templen As Integer = txtfind.SelectionLength

txtfind.Text = txtfind.Text.Remove(tempstart, templen)

txtfind.Text = txtfind.Text.Insert(tempstart, temprep)

txtfind.SelectionStart = tempstart

txtfind.SelectionLength = temprep.Length

pastend = FindIt()

Else

pastend = FindIt()

End If


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: http://www.blogger.com Changed to: http://technage.blogger.com
  • 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.

THE CODES:

FindForm.VB




FindForm.Designer.VB