有N
定义的话,这个问题3个字例如:open
icebreaker
umbrela
。首字母缩写词搜索正则表达式建设与perl
想知道这里的任何可能的缩写词是什么作为英语单词存在,例如,想运行是这样的:在上述正则表达式,然后我决定
grep -Pi '^o(p(e?))?i(c(e?))?um?$' my_long_wordlist.txt
我可以使用
- 从第一个字
o
,或op
或ope
(第一或前两个或前三个字母) - 从第2个字
i
,或ic
或ice
(第一或前两个或前三个字母) - ,最终形成的最后一个字我可以使用LY第一或firtst两个字母 -
u
或um
的乐趣 - 上述正则表达式将返回我的一句话:opium
:)
用手构建的正则表达式是一个两个测试可以接受的,但我想检查很多单词组合,所以,寻找如何生成正则表达式的方式如上。
要建立一个“的缩写取景器的正则表达式脚本”与以下电话:
acrobuild open:4 icebreaker:3 umbrela:3
正如你所看到的,在ARG游戏的话,和分隔符后面的数字是最大数信从一开始就可以在首字母缩写中使用什么。
现在的问题 - 我完全失去了如何建立给定长度的正则表达式。需要一些提示,想法或想。 - 勾选“需要帮助这里” :)
目前我有这样的:
#!/usr/bin/perl
use 5.012;
use strict;
use warnings;
do_grep( make_regex(@ARGV));
exit;
sub make_regex {
my(@words) = @_;
my $regex;
foreach my $wordnum (@words) {
$regex .= make_word_regex(split(/:/, $wordnum));
}
$regex = '^' . $regex . '$' if $regex;
return $regex;
}
sub make_word_regex {
my($word, $num) = @_;
return "" unless $word;
$num = length($word) unless defined($num); #for make legal -> word:0
my(@chars) = split(//, substr($word,0,$num));
#regex building x or xy? or x(y(z?))? etc... :(
my $re = "";
foreach my $c (reverse(@chars)) { #reverse, to building inside-out
# HOW TO BUILD THE REGEX HERE?
# NEED HELP HERE
}
return($re);
}
sub do_grep {
my($re) = @_;
say "$re"; return; #tmp
my $recomp = qr/$re/i;
open(my $fdict, "<", "/usr/share/dict/web2") or die("No dict file $!");
while(<$fdict>) {
chomp;
say $_ if m/$recomp/;
}
close($fdict);
}
问题的非常漂亮的简化。 – jm666