Whip Up Regular Expressions With Txt2regex
As the famous saying goes, "some people, when confronted with a problem, think, 'I know, I'll use regular expressions.' Now they have two problems." Wise words from Jamie Zawinski -- but sometimes you just can't avoid it. If regexes give you fits, consider breaking out txt2regex to muddle through.Understanding regular expressions is important for anyone who spends time doing system administration or programming. If that describes you and regexes are not your strong suit, txt2regex is an excellent tool to have handy.
A strong understanding of regular expressions is a very good thing for folks who spend much time doing system administration, programming or just a great deal of computer use in general. If regexes are not your strong suit, or you want to provide a tool for other users who are weak with regexes, then txt2regex might be a good tool to have handy.
What is txt2regex? It's a shell script that uses Bash's built-in commands to provide a text-based wizard to create regular expressions. You can grab the script from SourceForge or (on some Linux distros) just install the txt2regex package. I'd recommend grabbing the package if your distro has it, as the package comes with a man page in addition to the script itself.
After installing txt2regex, run
txt2regex. You'll find a fairly sparse menu with a list of programs and a cursor waiting for you to decide -- does the regex start to match on the beginning of the line, or on any part of the line? Continue answering the prompts until you've finished creating the regular expression you need. At that point, you can hit
. to quit. txt2regex will give you a description of the regular expression you've created with a list of appropriate expressions for different programs (e.g., Perl, PHP, sed and Vim) and a line like:
You may want to copy that, as it will regenerate the regular expression you've created.
txt2regex starts with a handful of programs it supports, but it actually has support for a lot more. While you're running
txt2regex, you can hit
txt2regex with support for the program you have in mind with
The downside to txt2regex? It's a bit long in the tooth -- it looks like the last update was in 2004. While the regular expressions for things like GNU sed change slowly, they do change on occasion. For example, GNU Awk 4.0.0 was released recently, and it includes a few minor changes to regular expressions. The program could probably do with a bit of an update -- but it is GPLv2 (or later), so anyone can update it if they so desire. Even with its age showing a bit, txt2regex remains a useful little tool that might save you some pouring over regex references when you're in a hurry.
Joe 'Zonker' Brockmeier is a freelance writer and editor with more than 10 years covering IT. Formerly the openSUSE Community Manager for Novell, Brockmeier has written for Linux Magazine, Sys Admin, Linux Pro Magazine, IBM developerWorks, Linux.com, CIO.com, Linux Weekly News, ZD Net, and many other publications. You can reach Zonker at firstname.lastname@example.org and follow him on Twitter.