Regex… Love it or Leave Programming
I love regular expressions, but I may be one of the few developers who doesn’t have a hate/hate relationship with them. Some guy once said,
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
Personally, I think that guy is… Well, just look at his homepage and you’ll figure it out.
My answer to people who hold this opinion (whether they are joking or super-cereal) is that regex is the language of parsing text. There are other ways to parse text, but they suck in comparison. Stop being lazy and learn it.
Still not in agreement? Let me put it another way,
Some people, when confronted with relational data, think “I know, I’ll use SQL.” Now they have two problems.
If you have the requirement to deal with relational data in your application, you’d better know SQL. SQL is the language of parsing relational data. Yes, there are other ways to parse and handle relational data. There is nothing to say that you can’t scan through data tables in a sequential manner using procedural code, but it would suck in comparison.
That’s not to say Regex or SQL is easy. But good things rarely are easy, and powerful things never are. With the right tools you can make them easier.
Moar Regex Tools, Plox
I’m a fan of good regex tools. In fact, I have five of them on my dev machine right now. They each offer a little something the others don’t. None of them are exactly what I need all the time.
I’ve been working on a regex tool that uses WPF for the UI that’s just about ready for its first release. I wanted to create a tool that was based as much as possible on xaml, DependencyObjects and databinding with a minimum of UI code. Its definitely not going to be the best damn regex tool anybody has ever seen; its purpose is primarily to be a learning experience for me, but if it ends up being my primary go-to tool for creating a regex, more the better.
I’m putting up some teasers because its so damn sweet. After I finish adding a few more tweaks the project will get a spot on CodePlex (should be within the next couple weeks).
My favorite regex tool is RegexDesigner.NET. Its bloat free, which gives it a very streamlined UI. Some people equate bells and whistles with quality; the truth is quality and pizzazz are not related at all. It is almost everything you need in a regex tool. You can set your regex options, enter sample text to parse, and perform matches, replacements and splits using the entered regex. Results are displayed in a simple format so the expression’s syntax can be quickly evaluated and adjusted. Errors in the expression result in a popup which shows the error message from the exception object.
I’d say the only thing missing would be an easy way to access the different elements of regex syntax for use in the expression under design. But if you’re looking for a simple regex tool, I’d definitely suggest this one.
My WPF Version
Here’s the app as it currently stands. I went a little nuts with the styles, as part of the purpose was to learn how skinning works in WPF. The template definitions are all in App.xaml, with specific changes scoped to the window level when they are used in all controls, leaving only individual tweaks inline. I’m getting a feel for keeping the layout UI as clean and small as possible, and unifying style definitions within the appropriate scope. It helps keep changing the layout as easy as possible and allows a single change in a single style setter to be reflected everywhere.
I created a DependencyObject that hooks directly into the UI that handles all regex parsing. The current version just does matching; replacing and splits will come later. Almost every UI element is hooked into a dependency property of this object. The object responds to updates to these properties in order to perform the regex parsing. You paste a bunch of text in to parse, type in your regex and immediately see all matches.
Hidden inside of an expander is a treeview with a help system/regex part selector. The data backing the treeview is stored in App.xaml. There is an XmlDataProvider that wraps an x:Data “XML island”. This island contains the raw data in XML format. The real sweet part about this setup is that each XML element is treated as a type in the markup. You can create individual HierarchicalDataTemplates for each element by setting the DataType property to the element name. Leaving off the x:Key for this template ensures it will be applied wherever you are binding against this element type automatically.
The treeview contains each regex syntax “part” organized by category. You can browse through the categories and view the information about each in a custom tooltip that is databound to that element’s properties. You can also click on an individual regex part and it will be either pasted at the carat in the regex box or replace the currently selected text. This is one of the few situations when markup and existing extensions couldn’t replace a simple event handler in code.
I’m still doing some polish work in my free time. I need to implement UI for doing replacements and splits, which might result in a restructuring of the overall UI. I also need to implement a UI for configuring the application (e.g., setting regex options). I’ve determined that there is a very large disconnect between items backed by collections and a Flags Enum (ListBox.SelectedItems and RegexOptions). Combine this with the need to store this value across sessions in the Properties object. I’d also like to provide a couple different skins, which would give me the opportunity to figure out the friggen theme system, which is really badly documented.
As I said, as soon as its decent I’ll pop it up on CodePlex. I’ll probably put up some posts covering some of the more interesting things I’ve learned from the project as well.
Eh, What the Heck
Here’s the 0.alpha.haqx0rz release to whet your whistle.