Skip to content

Instantly share code, notes, and snippets.

@nega0
Created July 24, 2019 14:22
Show Gist options
  • Save nega0/d35b35cb250ed34ce9194129312d03d2 to your computer and use it in GitHub Desktop.
Save nega0/d35b35cb250ed34ce9194129312d03d2 to your computer and use it in GitHub Desktop.
create an optimized regular expression (regex/regexp) from list of strings
#!/usr/bin/perl -w
use feature qw(say);
use strict;
use Regexp::List;
my @s = <STDIN>;
my $l = Regexp::List->new;
say $l->list2re(@s);
#!/usr/bin/env perl # -*- mode: cperl; indent-tabs-mode: nil; cperl-indent-level: 4 -*-
use feature qw(say state);
use warnings;
use strict;
use utf8;
use Regexp::Assemble;
my $r = Regexp::Assemble->new();
while (<>) {
$r->add( "$_" );
}
say $r->as_string;
## echo foo bar baz bif boo | xargs -n1 echo | ropt ==> (?:b(?:a[rz]|if|oo)|foo)
alias ropt="perl -MRegexp::Assemble -ne 'BEGIN { \$r = Regexp::Assemble->new(); } \$r->add(\"\$_\"); END { print \$r->as_string }'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment