harnir’s blog

Easier regexps

Today on cmdln.org blog I’ve found a short note about txt2regex, console regexp helper. txt2regex is an advanced script which helps you build regular expressions from scratch, using easy to use text menu - you can create your expression piece by piece. It supports currently (as of 2004 - yeah, haven’t been updated since) 23 applications and programming languages, from Perl and vim to OpenOffice.org. I’ve tried it for a while and it looks pretty handy - I don’t know how it will do with advanced regexps you can find in the wild, but it should be more than enough for converting regular expressions from one language to another.

Here’s an example session. After startup, txt2regex presents rather clean interface. On top and bottom you have possible choices and you can use the keyboard to navigate.

txt2regex after startup

Now let start our new regexp with start-of-line character to indicate that you are looking from the beginning of line (press 1). As you can see, list of choices has changed - now we can insert arbitrary characters, strings or special cases into our regular expression.

Beginning of regular expression

txt2regex automatically creates expressions for various languages, selectable at the beginning or from the command line - here, PostgreSQL uses different escape string than the rest of the applications selected. In many cases you can easily specify how many times you want specific character or string to appear in the expression.

Selecting repetition

Here’s the end of the session. After exiting, txt2regex prints a “history” of specified commands - using that you can recreate your expression in case you forgot it or want to test it with other languages.

End of session

Since it’s a really old script, you probably will find it in your distribution (it’s available at least in Debian and Ubuntu, as txt2regex of course). zsh prompt not included. :-)

Category: English, System administration

Tags: , , , ,

Leave a comment

CC-BY, 2009 Maciej Delmanowski