in reply to Re: matching with inclusion
in thread matching with inclusion
"sigma" matches "sig<SOMETHING>ma"can also be read as allowing for only a single insertion. That looks simpler, but the regex is harder to build. Here is one way:
It involves splitting $word in all pairs of non-empty substrings, something that comes up occasionally. I have yet to find a satisfactory solution for that. Using substr() like I did above is clear enough, but lengthy.my $word = 'sigma'; my $re = join '|' => map join( '.*?' => map quotemeta, @$_) => map [ substr( $word, 0, $_), substr( $word, $_)] => 1 .. length( $word) - 1; foreach my $text ( 'sigma', 'stigma', 'silly gamma', 'magma', ) { print("$text ", ($text =~ $re ? "matches" : "doesn't match"), "\n") +; }
is shorter but less readable.map { [ $word =~ /(.{$_})(.*)/] } 1 .. length( $word) - 1;
What else?
Update: No improvement either way, but fun:
Anno
|
|---|