Gojomo

2008-06-28
Animated regular-expression prime test in faster Regex Powertoy

This thread at News.YC motivated me to deploy some recent improvements to Regex Powertoy. It's now noticeably faster, especially for animating regex matching, and 'matchmarks' more reliably capture the entire syntax/display/animate settings.

Considering the prime-testing regex mentioned in the thread:

/^1?$|^(11+?)\1+$/
We can watch this match number 49 -- essentially discovering a factor and confirming that 49 is not prime -- by visiting this matchmark:
/^1?$|^(11+?)\1+$/
1111111111111111111111111111111111111111111111111
We can watch the regex fail to match 47 -- confirming its primality -- with this matchmark:
/^1?$|^(11+?)\1+$/
11111111111111111111111111111111111111111111111
We can further fancy things up by including a substitution string to replace not-prime numbers, and use the 'show edits' highlight mode:
s/^1?$|^(11+?)\1+$/not prime/
1111111111111111111111111111111111111111111111111
Note that clicking on the matched range brings up a detail view showing subgroup matches -- in this case the group's length is the factor found.

Note that Regex Powertoy requires Java -- a hidden applet makes use of Java's better-than-Javascript regex engine. (The recent changes have included replacing an old version of Prototype with the latest jQuery, and minimizing the slow JS-to-Java callouts by returning results in batches.)

Labels: , , , , , ,