我是新来的正则表达式。我需要一些帮助。使用正则表达式打印字母数字字符
我需要打印所有组合8个字符(字母数字),它们遵循以下模式。
First character must be alphabetic
Second characters must be numeric
Must not have 3 consecutive numeric characters
Must not have 4 consecutive alphabetic characters
我是新来的正则表达式。我需要一些帮助。使用正则表达式打印字母数字字符
我需要打印所有组合8个字符(字母数字),它们遵循以下模式。
First character must be alphabetic
Second characters must be numeric
Must not have 3 consecutive numeric characters
Must not have 4 consecutive alphabetic characters
正则表达式用于定义语法。匹配和替换运算符检查一个字符串是否匹配使用正则表达式定义的语法模式。我不知道任何使用正则表达式模式来生成字符串的工具。
当你有嵌套循环的任意号码,你想Algorithm::Loops的NestedLoops
。在你的情况下,有一个固定数量的循环,但有很多NestedLoops
有用。
[我假设你只在ASCII字母和数字有关]
娜ï VE(生成所有的序列,并过滤掉不需要的):
use Algorithm::Loops qw(NestedLoops);
my @syms = ('A'..'Z', 'a'..'z', '0'..'9');
my $iter = NestedLoops([ (\@syms) x 8 ]);
while (my @s = $iter->()) {
my $s = join('', @s);
next if $s !~ /^[a-zA-Z][0-9]/;
next if $s =~ /[a-zA-Z]{4}|[0-9]{3}/;
say $s;
}
晴娜ï VE(除产生明显坏的所有序列,并过滤掉不需要的):
use Algorithm::Loops qw(NestedLoops);
my @letters = ('A'..'Z', 'a'..'z');
my @digits = ('0'..'9');
my @syms = (@letters, @digits);
my $iter = NestedLoops([
\@letters,
\@digits,
(\@syms) x 6,
]);
while (my @s = $iter->()) {
my $s = join('', @s);
next if $s =~ /[a-zA-Z]{4}|[0-9]{3}/;
say $s;
}
但仍会产生很多需要扔掉的字符串。以下内容仅生成我们想要的字符串。它通过生成可生成所需字符串的模式(LDLLLDLL
,LDLLLDLD
,LDLLLDDL
,LDLLDLLL
,...),然后从这些模式中生成字符串。
高效的算法(这并不意味着实现有效):
use Algorithm::Loops qw(NestedLoops);
my @letters = ('A'..'Z', 'a'..'z');
my @digits = ('0'..'9');
sub make_pat_gen_iter {
my $raw_pat_gen_iter = NestedLoops([
['L'],
['D'],
(['L','D']) x 6,
]);
return sub {
while (1) {
my @pat = $raw_pat_gen_iter->();
return() if [email protected];
my $pat = join('', @pat);
next if $pat =~ /L{4}|D{3}/;
return @pat;
}
};
}
sub make_gen_iter {
my $pat_gen_iter = make_pat_gen_iter();
my @pat;
my $gen_sub_iter;
return sub {
return() if !$pat_gen_iter;
while (1) {
if (!$gen_sub_iter) {
@pat = $pat_gen_iter->();
if ([email protected]) {
$pat_gen_iter = undef;
return();
}
$gen_sub_iter = NestedLoops([
map { $_ eq 'L' ? \@letters : \@digits } @pat
]);
}
my @s = $gen_sub_iter->();
if ([email protected]) {
$gen_sub_iter = undef;
next;
}
return join('', @s);
}
};
}
my $gen_iter = make_gen_iter();
while (defined(my $s = $gen_iter->())) {
say $s;
}
为了好玩,下面是模式迭代器的完整结果:
LDLLLDLL LDLLLDLD LDLLLDDL LDLLDLLL LDLLDLLD
LDLLDLDL LDLLDLDD LDLLDDLL LDLLDDLD LDLDLLLD
LDLDLLDL LDLDLLDD LDLDLDLL LDLDLDLD LDLDLDDL
LDLDDLLL LDLDDLLD LDLDDLDL LDLDDLDD LDDLLLDL
LDDLLLDD LDDLLDLL LDDLLDLD LDDLLDDL LDDLDLLL
LDDLDLLD LDDLDLDL LDDLDLDD LDDLDDLL LDDLDDLD
我得到下面的输出无法在@INC中找到Algorithm/Loops.pm(您可能需要安装Algorithm :: Loops模块)(@INC包含: /usr/local/lib64/perl5/usr/local/share/perl5/usr/lib64/perl5/vendor_perl/usr/share/perl5/vendor_perl/usr/l ib64/perl5/usr/share/perl5。)at main.pl line 4 。 BEGIN失败 - 编译在main.pl第4行中止。 – user414977
您是否已安装模块? – ikegami
安装,现在越来越无法找到对象方法“说”通过包“A0AAA0AA”(也许你忘了加载“A0AAA0AA”?)在sr.pl线62. – user414977
您可以检查每个模式。 让我们命名字符串$ str。生成$ str从“00000000”到“zzzzzzzz”,并使用以下如果子句用于拾取与模式匹配的字符串。
if ($str =~ /^[A-Za-z][0-9]/ // pattern 1 and 2
&& $str !~ /[0-9]{3}/ // pattern 3
&& $str !~ /[A-Za-z]{4}/) // pattern 4
如何正则表达式参与? – Pradeep
它是使用正则表达式的选项 – user414977
正则表达式用于定义语法。匹配和替换运算符检查一个字符串是否匹配使用正则表达式定义的语法模式。我不知道任何使用正则表达式模式来生成字符串的工具。 – ikegami