2012-07-10 14 views
12

文字列 "aa \ nbb \ ncc"の最後の文字から最初の改行( "a")までを一致させたい複数行の文字列と

"aa\nbb\ncc" =~ qr/(. $ .+)/xms試合a\nbb\ncc

"aa\nbb\ncc\n" =~ qr/(. $ .+)/xms試合a\nbb\ncc\nものと予想しました。

しかし、私は"aa\nbb\ncc" =~ qr/(. $ .+)/xms の一致がなく、"aa\nbb\ncc" =~ qr/(. $ .+)/xmsの場合はc\nと一致します。

qr/(. $ ..+)/xmsを使用すると、予想される結果が得られました(コード例を参照)。

Perlバージョン5.14.2。

誰でもその動作について説明できますか?

はperldocはperlre:

次のサンプルコードを実行する
m Treat string as multiple lines. That is, change "^" and "$" 
     from matching the start or end of the string to matching the start 
     or end of any line anywhere within the string. 

    s Treat string as single line. That is, change "." to match any character 
     whatsoever, even a newline, which normally it would not match. 

     Used together, as "/ms", they let the "." match any character whatsoever, 
     while still allowing "^" and "$" to match, respectively, just after and 
     just before ewlines within the string. 

    \z Match only at end of string 

#!/usr/bin/env perl 

use strict; 
use warnings; 

print "Multiline string : ", '"aa\nbb\ncc"', "\n\n"; 
my $str = "aa\nbb\ncc"; 

print_match($str, qr/(. $)/xms);  # matches "a" 
print_match($str, qr/(. $ .)/xms);  # matches "a\n" 
print_match($str, qr/(. $ ..)/xms); # matches "a\nb" 
print_match($str, qr/(. $ ..+)/xms); # matches "a\nbb\ncc" 
print_match($str, qr/(. $ .+)/xms); # NO MATCH ! Why ??? 
print_match($str, qr/(. $ .+ \z)/xms); # NO MATCH ! Why ??? 

print "\nMultiline string now with terminating newline : ", '"aa\nbb\ncc\n"', "\n\n"; 
$str = "aa\nbb\ncc\n"; 

print_match($str, qr/(. $)/xms);  # matches "a" 
print_match($str, qr/(. $ .)/xms);  # matches "a\n" 
print_match($str, qr/(. $ ..)/xms); # matches "a\nb" 
print_match($str, qr/(. $ ..+)/xms); # matches "a\nbb\ncc\n" 
print_match($str, qr/(. $ .+)/xms); # MATCHES "c\n" ! Why ??? 
print_match($str, qr/(. $ .+ \z)/xms); # MATCHES "c\n" ! Why ??? 

sub print_match { 
    my ($str, $regex) = @_; 
    $str =~ $regex; 
    if ($1) { 
     printf "--> %-20s matched : >%s< \n", $regex, $1; 
    } 
    else { 
     printf "--> %-20s : no match !\n", $regex; 
    } 
} 

出力は次のようになります。

Multiline string : "aa\nbb\ncc" 

--> (?^msx:(. $))  matched : >a< 
--> (?^msx:(. $ .)) matched : >a 
< 
--> (?^msx:(. $ ..)) matched : >a 
b< 
--> (?^msx:(. $ ..+)) matched : >a 
bb 
cc< 
--> (?^msx:(. $ .+)) : no match ! 

Multiline string now with terminating newline : "aa\nbb\ncc\n" 

--> (?^msx:(. $))  matched : >a< 
--> (?^msx:(. $ .)) matched : >a 
< 
--> (?^msx:(. $ ..)) matched : >a 
b< 
--> (?^msx:(. $ ..+)) matched : >a 
bb 
cc 
< 
--> (?^msx:(. $ .+)) matched : >c 
< 
+0

これはPerlのバグのようです。良い発見!関連して: '' a \ nb "'は 'm/a $ .. \ z/ms'の代わりに' m/a $ ... \ z/ms'にマッチします。 *しかし*、ドットの周りにカッコを入れて何が起こっているのか理解しようとすると、突然m/a $(。)の代わりに 'm/a $(。)\ z/ms'と一致します。 。)(。)\ z/ms'。 – ruakh

+0

むしろ変です。 perl 5.12.2と同じです。 – katastrophos

答えて

8

それはバグです。 perlbugコマンドラインを実行して報告してください。あなたが言ったように

$ perl -E'say "aa\nbb\ncc" =~ qr/(. $ .+)/xms ? ">$1<" : 0' 
0 

$ perl -E'say "aa\nbb\ncc\n" =~ qr/(. $ .+)/xms ? ">$1<" : 0' 
>c 
< 

$ perl -v 
... 
This is perl 5, version 16, subversion 0 (v5.16.0) built for x86_64-linux 
... 

、彼らはそれぞれ"a\nbb\ncc""a\nbb\ncc\n"と一致する必要があります。 $に関連する最適化があります。そのうちの1人が/msを考慮に入れなかったようです。


PS —あなたはuse re 'debug';に興味があるかもしれません。

$ perl -Mre=debug -E'say "aa\nbb\ncc" =~ qr/(. $ .+)/xms ? ">$1<" : 0' 
Compiling REx "(. $ .+)" 
Final program: 
    1: OPEN1 (3) 
    3: SANY (4) 
    4: MEOL (5) 
    5: PLUS (7) 
    6:  SANY (0) 
    7: CLOSE1 (9) 
    9: END (0) 
anchored ""$ at 2 minlen 2 
Matching REx "(. $ .+)" against "aa%nbb%ncc" 
    0 <> <aa%nbb%ncc>   | 1:OPEN1(3) 
    0 <> <aa%nbb%ncc>   | 3:SANY(4) 
    1 <a> <a%nbb%ncc>   | 4:MEOL(5) 
            failed... 
    3 <aa%n> <bb%ncc>   | 1:OPEN1(3) 
    3 <aa%n> <bb%ncc>   | 3:SANY(4) 
    4 <aa%nb> <b%ncc>   | 4:MEOL(5) 
            failed... 
    6 <aa%nbb%n> <cc>   | 1:OPEN1(3) 
    6 <aa%nbb%n> <cc>   | 3:SANY(4) 
    7 <aa%nbb%nc> <c>   | 4:MEOL(5) 
            failed... 
Match failed 
0 
Freeing REx: "(. $ .+)" 
+0

perl 5.16.0も影響を受けます。私はバグ報告を送るでしょう。 Tnxは私に「デバッグ」を使用するよう指示します。 – katastrophos

+1

Perl 5.18の[修正済み](http://perl5.git.perl.org/perl.git/commitdiff/ac7af3f615eb56bda50bf123662b15779da26826?hp=79a3e5ea36208f2f54e36fa3a73c72808a6d0ad8) – ikegami