Relief from Regexes

As most of the readers on this blog know that my sys-admin skills are not on the high side. Well also one thing that I am very poor at is regexes. I know I hold my head in shame in this, I just never used them very much and most of my programming hasn't been in the area of text processing so I have never use them very much.

Most of the time when I run into a regex I can figure out what is going on but when the complex ones come into play I am just flustered.

Well I stumbled across this today Padre::Plugin::RegexExplain well not as powerful as my old standby The Regexe Coach

Well it was easy enough to install and when I called it up I got this

regex-1.jpg

Ok simple enough UI lets test it with a regex that always torments me is extracting dates out of a string.

Here is the one I usually have grief with

m|(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)|

that is suppose to extract out a date in this format '10/12/98 22:22:21' so I plug it in and here is what I get

regex-2.jpg

Ok that is neat I can even play around a bit so by clicking the the 'Show Description' checkbox I can see what the regex is trying to do.

I did try one other that came would of come in handy and that is give me the 'Package' name from a full namespace, (this being what ever is after the last set of '::'

So I looked up this '^.*::' and then tried this 'Test::Package::Mine' and got this

regex-3.jpg

Which left me a little confused as I wanted only the end text not all of it, a bug maybe but nothing is in the queue on it. Fortunately there is a 'Show Substitution' checkbox and giving that a quick click I can see that my regex worked as all I see is the 'Mine' in the 'Result from replace:' as I have this

regex-4.jpg

Ok it works fair enough and for simple ones I have a good tool I can use on the fly. I think for the complicated one I will stick with The Regex Coach.

4 Comments

BTW, I found the most usable "visual/GUI regexp debugger thingy" to be Komodo IDE's Rx toolkit. For the free alternative, I found kodos/kiki to be adequate. But you'll need to remember to write (?PAT) named captures as (?PPAT) because kodos/kiki only recognizes the latter (while Perl and Rx toolkit recognize both).


^ That would be (?<NAME>PAT) and (?P<NAME>PAT)

By the way, "Regex Editor" dialog is a core Padre feature that I contributed back in Padre 0.57. It has nothing to do with Padre::Plugin::RegexExplain.

So now you have used so far two of my contributions: Padre:Plugin:Moose and this regex dialog. Maybe now I should go back and work again on Farabi and Padre again :)

Big Thanks :)

I'm a big fan of rxrx, which is part of Damian Conway's Regexp::Debugger package. A downside is the lack of command line history, but that can be dealt with using rlwrap.

For those who haven't seen it in action, it steps through the regex engine displaying the results as it goes along. It is so cool! It's really illuminating, as it gives you a good sense of which parts of your expression are expensive and which are not.

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations