Thursday, August 6, 2009

Regular Expressions Coaching

Try as I might, I've never had instant recall on the details of regular expressions. For whatever reason (infrequent use, old age) the syntax just slides out of my head and onto the floor mere minutes after using it.

Awhile back a co-worker introduced me to The Regex Coach, and I've used it regularly ever since. Paste in a snippet of text, type a regex pattern, and it highlights matches in the text as you type. Super easy way to (re)learn or explore regex.


It's targeted at Perl-style regex, which for me has proved completely compatible with Python's re module. If you like it, don't forget to donate.

A bonus tip for the regex-challenged like myself...
If you memorize only one thing: (.*)

11 comments:

Jeiel Aranal said...

It's probably not as fleshed out as your friend's trainer but someone made a game out of regex in a previous PyWeek game programming contest.

http://pyweek.org/e/RegExExpress/

meastp said...

For linux users, there is a similar tool called kiki (although not as feature rich).

If you want to really learn regexes, though, or want a reference, try «Mastering Regular Expressions» from O'Reilly.

jcalderone said...

You really shouldn't use regular expressions to extract information from XML documents.

I know it's just an example, but it's suggestive, both of what you may be using regular expressions for and what other people may think regular expressions are useful for.

Use an XML parser. The ad-hoc regular expression you write is almost certainly harder, slower, and worst of all, wrong.

Brian said...

Expresso is even better. But, it's only on Windows, I think.

http://www.ultrapico.com/Expresso.htm

Paddy3118 said...

Kodos does more, in that it will als ocreate the code for you; and it does it in Python!

http://kodos.sourceforge.net/

- Have fun.

Hal said...

Why an app? I've used retest for years.

If you have python & a browser, you can use it locally. Otherwise, there's a web version.

--Hal

Peter Stråhle said...

The regex app and retest doesn't seem so much alike? The regex app you can highlight what you want the expression to match. Retest is only a tester for the expressions.

loocas said...

Thank you for the tip! Very handy.

Anonymous said...

Try Regex Buddy.

irrigger said...

Or utilize an editor like Vim that integrates regular expressions into everyday use.


:set incsearch
for incrementa searches

to start the search use / so your expression would be.

/^.\+foo.\+$

renatopolimeno said...

I´ve been using this http://gskinner.com/RegExr
for quite while ;)
Give it a try!

Cheers,