2013-02-13 14:24:28 -08:00
|
|
|
package DDG::Goodie::Regexp;
|
2014-08-20 11:45:33 -07:00
|
|
|
# ABSTRACT: Parse a regexp.
|
2013-02-13 14:24:28 -08:00
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
use strict;
|
2013-02-13 14:24:28 -08:00
|
|
|
use DDG::Goodie;
|
2014-08-20 11:45:33 -07:00
|
|
|
|
2013-02-13 14:24:28 -08:00
|
|
|
use Safe;
|
|
|
|
|
|
|
|
zci answer_type => "regexp";
|
2014-09-27 06:42:57 -07:00
|
|
|
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' ],
|
2015-01-07 10:24:47 -08:00
|
|
|
github => [ 'duckduckgo', 'DuckDuckGo'],
|
|
|
|
twitter => ['duckduckgo', 'DuckDuckGo'];
|
2013-02-14 12:31:50 -08:00
|
|
|
|
2013-11-05 05:34:26 -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);
|
|
|
|
};
|
|
|
|
|
2013-03-19 09:17:12 -07:00
|
|
|
return join( ' | ', @results ), heading => 'Regexp Result' if @results;
|
|
|
|
return;
|
2013-02-13 14:24:28 -08:00
|
|
|
};
|
|
|
|
|
2014-08-20 11:45:33 -07:00
|
|
|
1;
|