Simplifying regex OR patterns

后端 未结 3 1613
后悔当初
后悔当初 2021-02-09 23:57

I was asked today if there was a library to take a list of strings and to compute the most efficient regex to match only those strings. I think it\'s an NP Complete problem by i

3条回答
  •  情深已故
    2021-02-10 00:32

    The Regex::PreSuf module is designed to do exactly this.

    To quote the Synopsis:

    use Regex::PreSuf;
    
    my $re = presuf(qw(foobar fooxar foozap));
    
    # $re should be now 'foo(?:zap|[bx]ar)'
    

提交回复
热议问题