zeroclickinfo-goodies/lib/DDG/Goodie/Regexp.pm

45 lines
1.0 KiB
Perl
Raw Normal View History

2013-02-13 14:24:28 -08:00
package DDG::Goodie::Regexp;
# ABSTRACT: Parse a regexp.
2013-02-13 14:24:28 -08:00
use strict;
2013-02-13 14:24:28 -08:00
use DDG::Goodie;
2013-02-13 14:24:28 -08:00
use Safe;
zci answer_type => "regexp";
zci is_cached => 1;
2013-02-13 14:24:28 -08:00
2013-02-14 12:31:50 -08:00
primary_example_queries 'regexp /(.*)/ ddg';
description 'Regular expressions';
name 'regexp';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Regexp.pm';
2013-02-14 12:48:41 -08:00
category 'computing_tools';
2013-02-14 12:31:50 -08:00
topics 'programming', 'sysadmin';
attribution web => [ 'https://www.duckduckgo.com', 'DuckDuckGo' ],
github => [ 'duckduckgo', 'DuckDuckGo'],
twitter => ['duckduckgo', 'DuckDuckGo'];
2013-02-14 12:31:50 -08:00
triggers query_lc => qr/^regex[p]? [\/\\](.+?)[\/\\] (.+)$/i;
2013-02-13 14:24:28 -08:00
handle query => sub {
my $regexp = $1;
my $str = $2;
my $compiler = Safe->new->reval(q{ sub { qr/$_[0]/ } });
sub compile_re {
my ( $re, $compiler ) = @_;
$compiler->($re);
}
my @results = ();
eval {
@results = $str =~ compile_re($regexp, $compiler);
};
return join( ' | ', @results ), heading => 'Regexp Result' if @results;
return;
2013-02-13 14:24:28 -08:00
};
1;