【问题标题】:Print alpha-numeric characters using regular expressions使用正则表达式打印字母数字字符
【发布时间】:2015-01-05 12:37:56
【问题描述】:

我是正则表达式的新手。

我需要打印所有符合以下模式的 8 个字符组合(字母数字)。

  • 第一个字符必须是字母
  • 第二个字符必须是数字
  • 不能有 3 个连续的数字字符
  • 不得有 4 个连续的字母字符

【问题讨论】:

  • 正则表达式如何参与?
  • 这是一个使用正则表达式的选项
  • 正则表达式用于定义语法。匹配和替换运算符检查字符串是否匹配使用正则表达式定义的语法模式。我不知道有任何工具使用正则表达式模式来生成字符串。
  • all combination 8 characters (alphanumeric) 大约是62**8。要打印所有这些,您将使用大量纸张。
  • 是的,所有的组合都会产生巨大的数字。我的目标是生成所有这些并存储在数据库中。但现在只需要打印 10。

标签: regex perl shell


【解决方案1】:

正则表达式用于定义语法。匹配和替换运算符检查字符串是否匹配使用正则表达式定义的语法模式。我不知道有任何工具使用正则表达式模式来生成字符串。


当您有任意数量的嵌套循环时,您需要Algorithm::LoopsNestedLoops。在您的情况下,循环的数量是固定的,但有太多的循环,NestedLoops 无论如何都会派上用场。

[我假设您只对 ASCII 中的字母和数字感兴趣。]

Naï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;
}

非常幼稚(生成除了明显不好的序列之外的所有序列,并过滤掉不需要的序列):

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;
}

但这仍然会产生很多需要丢弃的字符串。下面只生成我们想要的字符串。它通过生成将产生所需字符串的模式(LDLLLDLLLDLLLDLDLDLLLDDLLDLLDLLL、...),然后从这些模式构建字符串。

高效算法(不代表实现高效):

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 !@pat;

         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 (!@pat) {
               $pat_gen_iter = undef;
               return ();
            }

            $gen_sub_iter = NestedLoops([
               map { $_ eq 'L' ? \@letters : \@digits } @pat
            ]);
         }

         my @s = $gen_sub_iter->();
         if (!@s) {
            $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

【讨论】:

  • 我得到以下输出Can't locate Algorithm/Loops.pm in @INC(您可能需要安装 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 .) 在 main.pl 第 4 行。开始失败- -编译在 main.pl 第 4 行中止。
  • 模块安装了吗?
  • 已安装,现在在 sr.pl 第 62 行通过包“A0AAA0AA”(也许您忘记加载“A0AAA0AA”?)找不到对象方法“say”。
  • 我得到了它的工作thnx mate!它将帮助我在项目中取得进展
  • 添加use strict; use warnings; use feature qw( say );。您也可以改用print。 (但如果你这样做,仍然添加use strict; use warnings;。)
【解决方案2】:

您可以检查每个模式。 我们将字符串命名为 $str。从 "00000000" 到 "zzzzzzzz" 生成 $str 并使用以下 if 子句来选择匹配模式的字符串。

if ($str =~ /^[A-Za-z][0-9]/        // pattern 1 and 2
  && $str !~ /[0-9]{3}/             // pattern 3
  && $str !~ /[A-Za-z]{4}/)         // pattern 4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多