jump to navigation

^txt2regex$: Regular Expression Tool To Create Regex from Description April 21, 2007

Posted by Carthik in applications, commands, packages, ubuntu.
trackback

^txt2regex$ is a lifesaver. It helps you create regular expression strings in a step by step process, by describing what your regex pattern should do in English (or your own language). The tool can create RegExes for use with 23 different programs, including sed, vim, mysql, and procmail. When you start the program, it will ask you a series of questions like “1. do you want to start matching at the beginning of lines? or 2. search anywhere?” and “this is followed by…. 1. A specific character…” etc… download it and run it and you will see.

Anyone who has worked with regular expressions for searching and optionally replacing stuff in files knows what a godsend then can be if you get the regex down pat – but they would also know what a time sink they can be if you can’t whip up exactly what you want. In the past, when faced with this kind of a situation, I would read man pages, books, experiment, fail and then, finally, succeed after a good half hour or so. txt2regex is a tool that eliminates the confusion. Totally.

You can install txt2regex on Ubuntu by doing a:
$sudo apt-get install txt2regex

Among its features include the ability to print a list of characteristics of the regular expression syntax for various tools, a history tool which keeps track of you past regexes, and some pre-built regexes that are often used – for dates, times and numbers.

For example,
$txt2regex --all --make number3
will create the regex for all supported tools. The regex will match a number of the form “34,412,069.90”

Here’s the output:

carthik@milan:~$ txt2regex --all --make number3

### number LEVEL 3: level 2 plus optional commas, like: 34,412,069.90

 RegEx awk       : [+-]?[0-9]!!(,[0-9]!!)*(\.[0-9]!!)?
 RegEx ed        : [+-]\?[0-9]\{1,3\}\(,[0-9]\{3\}\)*\(\.[0-9]\{2\}\)\?
 RegEx egrep     : [+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{2})?
 RegEx emacs     : [+-]?[0-9]!!\(,[0-9]!!\)*\(\.[0-9]!!\)?
 RegEx expect    : [+-]?[0-9]!!(,[0-9]!!)*(\.[0-9]!!)?
 RegEx find      : [+-]?[0-9]!!\(,[0-9]!!\)*\(\.[0-9]!!\)?
 RegEx gawk      : [+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{2})?
 RegEx grep      : [+-]\?[0-9]\{1,3\}\(,[0-9]\{3\}\)*\(\.[0-9]\{2\}\)\?
 RegEx javascript: [+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{2})?
 RegEx lex       : [+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{2})?
 RegEx lisp      : [+-]?[0-9]!!\\(,[0-9]!!\\)*\\(\\.[0-9]!!\\)?
 RegEx mawk      : [+-]?[0-9]!!(,[0-9]!!)*(\.[0-9]!!)?
 RegEx mysql     : [+-]?[0-9]{1,3}(,[0-9]{3})*(\\.[0-9]{2})?
 RegEx ooo       : [+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{2})?
 RegEx perl      : [+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{2})?
 RegEx php       : [+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{2})?
 RegEx postgres  : [+-]?[0-9]{1,3}(,[0-9]{3})*(\\.[0-9]{2})?
 RegEx procmail  : [+-]?[0-9]!!(,[0-9]!!)*(\.[0-9]!!)?
 RegEx python    : [+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{2})?
 RegEx sed       : [+-]\?[0-9]\{1,3\}\(,[0-9]\{3\}\)*\(\.[0-9]\{2\}\)\?
 RegEx tcl       : [+-]?[0-9]!!(,[0-9]!!)*(\.[0-9]!!)?
 RegEx vbscript  : [+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]{2})?
 RegEx vi        : [+-]\{0,1\}[0-9]\{1,3\}\(,[0-9]\{3\}\)*\(\.[0-9]\{2\}\)\{0,1\}
 RegEx vim       : [+-]\=[0-9]\{1,3}\(,[0-9]\{3}\)*\(\.[0-9]\{2}\)\=

$txt2regex --help
prints out a short help message

and

man txt2regex
gives some more info.

What would be handy is if txt2regex had an extension that allowed one to deconstruct a regex – give it a regex and it tells you what it does in plain English. Also, I cannot seem to create regexes for the mod_rewrite module in apache. I suspect that since mod_rewrite supports POSIX regexes, I could just run with one or the other of the regexes created by txt2regex. Since I haven’t tried it, I can’t say which one of the 23, just yet.

Comments»

1. Caio Felipe - April 21, 2007

The Author of this script is Aurelio Marinho Jargas, great Brazilian Programmer. He made a lot of other useful scripts and have a great site full of Bash Scripts and Regexp references.

Visit: http://aurelio.net (In Portuguese) or
http://aurelio.net/en/ (in English)

Great Article By The Way

2. Rahul Batra - April 21, 2007

For constructing/deconstructing a regular expresion try kregexpeditor in KDE (comes pre-installed by default in almost all KDE based distros).
kregexpeditor is an awesome tool and comes as a real life saver in many situations.

3. Adam Chodorowski - April 21, 2007

It’s a pity it generates incorrect regexps. For example, most regexps in the example don’t escape “.” (so that will match any character), “=” doesn’t work as “?” in vim, find needs \-escaping of “(” and “)”, … Also, how can “!!” be equivalent to “{2}”, {3}” *and* “{1,3}”? That doesn’t make sense.

4. Alex - April 23, 2007

More to the point why is there a ‘find’ entry? To the best of my knowledge, find gives you a choice of regex implementations, the default being emacs. (And yes the parens do need escaping.)

5. Axl - May 21, 2007

Adam,
I’ve run the command from the post and here all the escapes were right.
I think they were “eaten” by the WordPress engine, when writing the post.

6. jeu de machines a sous - October 23, 2007

Olympic jouer is some false training. It’s compatible to be drank! The sous is abruptly annual. Pound bit the million. I cowered that food near one boy. Western ground is one stable car.
jeu de machines a sous – http://www.slot-machine-en-ligne.fr/

7. Yacht rental - November 18, 2009

He made a lot of other useful scripts and have a great site full of Bash Scripts and Regexp references.

8. Alquiler de yates de lujo - November 18, 2009

I’ve run the command from the post and here all the escapes were right.
I think they were “eaten” by the WordPress engine, when writing the post.

9. Alquiler de barcos en Ibiza - March 25, 2010

More to the point why is there a ‘find’ entry? To the best of my knowledge, find gives you a choice of regex implementations, the default being emacs.

10. Alquiler de barcos en Ibiza - April 29, 2010

For constructing/deconstructing a regular expresion try kregexpeditor in KDE (comes pre-installed by default in almost all KDE based distros).

11. travesti - June 29, 2010

ttf-gentium is now ttf-sil-gentium in the repository

12. travesti - June 29, 2010

post thank

13. Alquiler de barcos en Ibiza - July 21, 2010

Olympic jouer is some false training. It’s compatible to be drank! The sous is abruptly annual. Pound bit the million. I cowered that food near one boy. Western ground is one stable car.

14. sikiş izle - September 26, 2010

For constructing/deconstructing a regular expresion try

15. antalya ilaçlama - September 27, 2010

More to the point why is there a ‘find’ entry? To the best of my knowledge, find gives you a choice of regex implementations, the default being emacs.

16. porno sikiş - September 27, 2010

kregexpeditor is an awesome tool and comes as a real life saver in many situations.

17. Brian - September 27, 2010

What would be handy is if txt2regex had an extension that allowed one to deconstruct a regex – give it a regex and it tells you what it does in plain English.

18. sex sikiş - September 28, 2010

Adam,
I’ve run the command from the post and here all the escapes were right.
I think they were “eaten” by the WordPress engine, when writing the post.

19. porno - November 1, 2010

Googled for “how to install web core fonts ubuntu” and your blog was second in the results list. porno izle thanks

20. kayseri nakliyat firmaları - October 12, 2011

güzel bir makale, bizimle paylaştığınız için teşekkür ederiz ..

21. current news updates 2011 to 2012 - November 25, 2011

this is very good blog you have running here, i do a news blog if anyone would like to check it out the link is below to click

current up to date news stories

22. çiğli temizlik şirketleri - November 5, 2013

nıye bole keyıfsızım benn

23. Jackson - November 1, 2014

Hm… This is great

24. ^txt2regex$:正则表达式编写向导-Linux学习 - October 12, 2015

[…] [via] […]


Leave a Reply to Alex Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: