tags:

views:

846

answers:

10

I need a web page or Windows application that takes a regular expression and explains it to me in text what it does.

Do you have any suggestions for such an application?

+3  A: 

The Regular Expression Workbench explains regular expressions in pseudocode (via the "Interpret" button).

Andrew Hare
+4  A: 

I quite like Eric Gunnerson's Regex Workbench. It's simple, free, and does this.

Greg Beech
the downloaded zip of his project contains a setup that doesn't work (tells me I need .Net 1.1 even though it's installed, as are several other versions). and it also doesn't contain the binary itself, so you have to build it yourself.
Assaf Lavie
I actually think it's best to build it yourself as the Regex implementation has probably changed slightly over the different versions of .NET, so by building it against the version of the framework you use, you'll get exactly the same behaviour in your application.
Greg Beech
+3  A: 

YAPE::Regex::Explain comes with a utility named explain that does this. I believe there is also a builit-in layer in later versions of Perl that also does something similar.

These tools are generally not very useful because, like bad comments, they tell you what a regex does, not why it is doing it.

Chas. Owens
It's probably true that no tool could guess the WHY of a Regex. With that limitation YAPE::Regex::Explain is as good as anything I've seen.
Ed Guiness
It is pretty nice, but I find tackling a large regex a piece at a time with an eye toward why it is doing what it is doing to much more useful, see http://stackoverflow.com/questions/708399 and http://stackoverflow.com/questions/708270 for examples of both approaches and tell me which is more useful. Explainers are a crutch, and if you learn using a crutch you may wind up limping your whole life (or at least until you realize that you can actually run).
Chas. Owens
+2  A: 

RegEx Buddy has an analyze feature.

And is recommended by Jeff.

eKek0
+1. I doubt you'll find anything better.
Lieven
Too bad it is not free. Only a 7 day trial
Germstorm
+2  A: 

Python has built-in support for this, using re.DEBUG or the sre_parse module:

>>> import re
>>> r = re.compile('(h\S+) (wo\S+)', re.DEBUG)
subpattern 1
  literal 104
  max_repeat 1 65535
    in
      category category_not_space
literal 32
subpattern 2
  literal 119
  literal 111
  max_repeat 1 65535
    in
      category category_not_space

>>> import sre_parse
>>> sre_parse.parse('(h\S+) (wo\S+)')
[('subpattern', (1, [('literal', 104), ('max_repeat', (1, 65535, [('in', [('category', 'category_not_space')])]))])), ('literal', 32), ('subpattern', (2, [('literal', 119), ('literal', 111), ('max_repeat', (1, 65535, [('in', [('category', 'category_not_space')])]))]))]

But the output of Regex buddy looks a bit nicer, I confess... However, it should not be too hard to transform the output of sre_parse.parse into something similar.

codeape
A: 

Edi Weitz's regex coach does this, and more.

simon
+1  A: 

Expresso is a great one I use all the time.

Ian Jacobs
+3  A: 

Perl has a wonderful Regex parser that will explain a regex in detail, using plain English as far as possible.

YAPE::Regex::Explain

See this answer for an example.

Ed Guiness
+1  A: 

Perl has a built in way to do something similar:

perl -Mre=debug -e"'aaabbbccdd'=~/a[b]+c?/"
# at Compile time
Compiling REx "a[b]+c?"
Final program:
   1: EXACT <a> (3)
   3: PLUS (6)
   4:   EXACT <b> (0)
   6: CURLY {0,1} (10)
   8:   EXACT <c> (0)
  10: END (0)
anchored "ab" at 0 floating "b" at 1..2147483647 (checking anchored) minlen 2 

# at run time
Guessing start of match in sv for REx "a[b]+c?" against "aaabbbccdd"
Found anchored substr "ab" at offset 2...
Found floating substr "b" at offset 3...
Starting position does not contradict "/^/m"...
Guessed: match at offset 2
Matching REx "a[b]+c?" against "abbbccdd"
   2 <aa> <abbbccdd>         |  1:EXACT <a>(3)
   3 <aaa> <bbbccdd>         |  3:PLUS(6)
                                  EXACT <b> can match 3 times out of 2147483647...
   6 <aaabbb> <ccdd>         |  6:  CURLY {0,1}(10)
                                    EXACT <c> can match 1 times out of 1...
   7 <aaabbbc> <cdd>         | 10:    END(0)
Match successful!

# at exit time
Freeing REx: "a[b]+c?"

use re 'debug';
Brad Gilbert
Interesting, Brad. Thanks. More on this at http://perldoc.perl.org/perldebug.html#Debugging-regular-expressions
Philip Durbin
+2  A: 

strfriend is an online regex viewer and works pretty well.

Here are some examples (via tr.im; SO borks on these links):

Gavin Miller
your examples are missing
Don Wakefield