3 ms·
I'm currently working on a ruby gem to generate examples of given regular expression. For example: /this|is|awesome/.examples # => ["this", "is", "awesome
by tom-lord 12y ago
I'm currently working on a ruby gem to generate examples of given regular expression. For example:
/this|is|awesome/.examples # => ["this", "is", "awesome"]
So, I tried it out on this "prime number validator" regex:
/1?|(11+?)\1+/.examples
#=> ["", "1", "1111", "111111", "11111111"]
/1?|(11+?)\1+/.examples(max_repeater_variance: 50, max_group_results: 100).uniq.sort.map(&:length).first(10)
#=> [0, 1, 4, 6, 8, 9, 10, 12, 14, 15]
require 'prime'
Array(1..100) - /1?|(11+?)\1+/.examples(max_repeater_variance: 50, max_group_results: 3000).uniq.sort.map(&:length) == Prime.first(25)
#=> true
Horribly inefficient, but pretty cool! Here's my gem if case anyone's interested: https://github.com/tom-lord/regexp-examples https://github.com/tom-lord/regexp-examples
- arielpts 12y agoThank you very much for your great gem :) Very usefull!
- tom-lord 12y agoThanks :) It's still "under development", while I add a few missing features, but as you can see from the README these are only for fairly obscure parts of the regex language! - E.g. POSIX groups, named properties, character set intersection, ...