例如,字崩溃将工作由于debac,但海底是行不通的,因为:1.有在可形成的任何5个字符的序列不c和2.字母e出现两次.作为另一个例子,由于edbac,反馈将起作用.请记住,解决方案必须仅使用正则表达式完成.
我试图实施的策略是:匹配第一个字母,如果它在[ae]内,并记住它.然后在[ae]中找到下一个字母,但不是第一个字母.等等.我不确定语法是什么(或者甚至存在某些语法)所以我的代码不起作用:
open(DICT, "dictionary.txt");
@words = <DICT>;
foreach my $word(@words){
if ($word =~ /([a-e])([a-e^\1])([a-e^\1^\2])([a-e^\1^\2^\3])([a-e^\1^\2^\3^\4])/
){
print $word;
}
}
Run Code Online (Sandbox Code Playgroud)
我也在考虑使用(?= regex)和\ G但是我不确定它是如何工作的.
ike*_*ami 15
/
(?= .{0,4}a )
(?= .{0,4}b )
(?= .{0,4}c )
(?= .{0,4}d )
(?= .{0,4}e )
/xs
Run Code Online (Sandbox Code Playgroud)
这可能会导致更快的匹配,从而生成所有组合的模式.
use Algorithm::Loops qw( NextPermute );
my @pats;
my @chars = 'a'..'e';
do { push @pats, quotemeta join '', @chars; } while NextPermute(@chars);
my $re = join '|', @pats;
Run Code Online (Sandbox Code Playgroud)
ABCDE | abced | abdce | ABDEC | ABECD | abedc | acbde | acbed | acdbe | acdeb | acebd | acedb | adbce | adbec | adcbe | adceb | adebc | adecb | aebcd | aebdc | aecbd | aecdb | aedbc | aedcb | bacde | baced | badce | badec | baecd | baedc | bcade | bcaed | bcdae | BCDEA | bcead | bceda | bdace | bdaec | bdcae | bdcea | bdeac | bdeca | beacd | beadc | becad | becda | bedac | bedca | cabde | cabed | cadbe | cadeb | caebd | caedb | cbade | cbaed | cbdae | cbdea | cbead | cbeda | cdabe | cdaeb | cdbae | cdbea | cdeab | cdeba | ceabd | ceadb | cebad | cebda |中央评价数据库| cedba | dabce | dabec | dacbe | daceb | daebc | daecb | dbace | dbaec | dbcae | dbcea | dbeac | dbeca | dcabe | dcaeb | dcbae | dcbea | dceab | dceba | DEABC | deacb | debac | debca | decab | decba | EABCD | eabdc | eacbd | eacdb | eadbc | eadcb | ebacd | ebadc | ebcad | ebcda | ebdac | ebdca | ecabd | ecadb | ecbad | ecbda | ecdab | ecdba | edabc | edacb | edbac | edbca | edcab | EDCBA
(这将在Perl 5.10+中优化为trie.在5.10之前,使用Regexp :: List.)
您的解决方案很聪明,但遗憾的[a-e^...]是,您找不到它.我不相信有一种方法可以混合常规和否定的字符类.我可以想到使用前瞻的解决方法:
/(([a-e])(?!\2)([a-e])(?!\2)(?!\3)([a-e])(?!\2)(?!\3)(?!\4])([a-e])(?!\2)(?!\3)(?!\4])(?!\5)([a-e]))/
Run Code Online (Sandbox Code Playgroud)
请在此处查看:http://rubular.com/r/6pFrJe78b6.
更新: Mob在下面的评论中指出,交替可用于压缩上述内容:
/(([a-e])(?!\2)([a-e])(?!\2|\3)([a-e])(?!\2|\3|\4])([a-e])(?!\2|\3|\4|\5)([a-e]))/
Run Code Online (Sandbox Code Playgroud)
新演示:http://rubular.com/r/UUS7mrz6Ze.
#! perl -lw
for (qw(debacle seabed feedback)) {
print if /([a-e])(?!\1)
([a-e])(?!\1)(?!\2)
([a-e])(?!\1)(?!\2)(?!\3)
([a-e])(?!\1)(?!\2)(?!\3)(?!\4)
([a-e])/x;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
969 次 |
| 最近记录: |