Created
July 24, 2019 14:22
-
-
Save nega0/d35b35cb250ed34ce9194129312d03d2 to your computer and use it in GitHub Desktop.
create an optimized regular expression (regex/regexp) from list of strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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