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?
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?
The Regular Expression Workbench explains regular expressions in pseudocode (via the "Interpret" button).
I quite like Eric Gunnerson's Regex Workbench. It's simple, free, and does this.
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.
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.
Perl has a wonderful Regex parser that will explain a regex in detail, using plain English as far as possible.
See this answer for an example.
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';
strfriend is an online regex viewer and works pretty well.
Here are some examples (via tr.im; SO borks on these links):