2015-01-05 48 views
1

我是新來的正則表達式。我需要一些幫助。使用正則表達式打印字母數字字符

我需要打印所有組合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 
+0

如何正則表達式參與? – Pradeep

+0

它是使用正則表達式的選項 – user414977

+0

正則表達式用於定義語法。匹配和替換運算符檢查一個字符串是否匹配使用正則表達式定義的語法模式。我不知道任何使用正則表達式模式來生成字符串的工具。 – ikegami

回答

1

正則表達式用於定義語法。匹配和替換運算符檢查一個字符串是否匹配使用正則表達式定義的語法模式。我不知道任何使用正則表達式模式來生成字符串的工具。


當你有嵌套循環的任意號碼,你想Algorithm::LoopsNestedLoops。在你的情況下,有一個固定數量的循環,但有很多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 
+0

我得到下面的輸出無法在@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

+0

您是否已安裝模塊? – ikegami

+0

安裝,現在越來越無法找到對象方法「說」通過包「A0AAA0AA」(也許你忘了加載「A0AAA0AA」?)在sr.pl線62. – user414977

0

您可以檢查每個模式。 讓我們命名字符串$ 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 
相關問題