From: matz@... (Yukihiro Matsumoto) Date: 1998-02-18T11:00:24+09:00 Subject: [ruby-list:6490] Re: gsub まつもと ゆきひろです どうも正規表現周りは昔からバグが多くて迷惑かけてます.(_ _) In message "[ruby-list:6484] Re: gsub" on 98/02/18, Tadayoshi Funaba writes: |ふなばです。 | |もうひとつ。改行コードで終っている文字列で、\Z がうまくマッチしないみたい |なんですが、これはどうですか? いや,実は先日Perl5本を見てて, \Zは文字列末尾直前の改行にもマッチする と書いてあってなんのことかなあとずっと思っていたんですが,ちょ うどゆうべふなばさんが指摘した問題があるからだと気がついて, 修正しました.これもパッチつけておきます. # Pythonのpcreはこのことを考慮していないみたいなんだがなあ. |日本語をつかったときも \Z がうまくないみたいなんです。たとえば、 | | "日本語".gsub!(/(\A|\Z)/, '%') | |といったもの。これも日本語でなければ思ったとおりなんです。 これもパッチをつけます. --- regex.c 1998/01/16 12:19:12 1.1.1.2 +++ regex.c 1998/02/18 01:56:41 1.1.1.2.2.2 @@ -3007,5 +3008,8 @@ - /* Match at the very end of the data. */ + /* Match at the very end of the data. */ case endbuf: if (AT_STRINGS_END(d)) + break; + /* .. or newline just before the end of the data. */ + if (*d == '\n' && AT_STRINGS_END(d+1)) break; --- string.c 1998/02/18 01:56:42 1.1.1.2.2.6 +++ string.c 1998/02/18 01:58:29 @@ -900,2 +900,3 @@ if (BEG(0) == END(0)) { + int len = ismbchar(RSTRING(str)->ptr[END(0)])?2:1; /* @@ -905,5 +906,5 @@ if (RSTRING(str)->len > END(0)) { - str_cat(result, RSTRING(str)->ptr+END(0), 1); + str_cat(result, RSTRING(str)->ptr+END(0), len); } - offset = END(0)+1; + offset = END(0)+len; }