From: Kikutani Makoto Date: 1999-04-11T15:01:55+09:00 Subject: [ruby-list:13593] Re: /.*\n$/ 毎度詳しい解説ありがとうございます。 Sun, Apr 11, 1999 at 02:13:23PM +0900 において Wakou Aoyama さん曰く: > まあ、それは別として、元々は Ruby のマッチングは Perl の //m 相当であ > り、/$/ は次のようにマッチしていました。 そうそう、ことあるごとにこれ(Perlの //m)言われて思い出しますが、 またすぐ忘れてしまう。FAQにあったほうがいい気が >前田さん > 従って、/\n$/ はもはやまったくマッチしない表現となりますので、そのかわ > りに、文字列の末尾を表す /\z/ が導入されました。 \Z でなく \z ですか? 1.2のマニュアルには記述がないです。 あと、Perlは "hoge\n" =~ /.*\n$/m がマッチするようです。 > ですが、同じ 1.2 系でもバージョン依存があるのはちょっと気になりますね。 > 週明けには 1.2.5 が出るようなので、そちらの様子を見てからの方が良いか > もしれません。 お、もう1.2.5ですか。profile.rb は入ったので、新しいtelnet.rbが 入ることも期待してしまおう。 Sun, Apr 11, 1999 at 02:24:03PM +0900 において Wakou Aoyama さん曰く: > /\n\Z/ では最後が空行だとまずいですね。 > > $ /usr/src/ruby-1.2.3/ruby -e 'p "aa\n\nbb\n\n".gsub(/\n\Z/, "\n--")' > "aa\n\nbb\n--\n--" > > /\n\Z(?!\n)/ にすべきでした。 正規表現は一応Perlで鍛えられたと思ってたけど、このあたり (?: ) 後方参照を伴わないグループ化 (?= ) パターンによる位置指定(幅を持たない) (?! ) パターンの否定による位置指定(幅を持たない) はさっぱりわかりません。man perlre して (?!regexp) A zero-width negative lookahead assertion. For example /foo(?!bar)/ matches any occurrence of "foo" that isn't followed by "bar". Note however that lookahead and lookbehind are NOT the same thing. You cannot use this for lookbehind: /(?!foo)bar/ will not find an occurrence of "bar" that is preceded by something which is not "foo". That's because the (?!foo) is just saying that the next thing cannot be "foo"--and it's not, it's a "bar", so "foobar" will match. You would have to do something like /(?!foo)...bar/ for that. We say "like" because there's the case of your "bar" not having three characters before it. You could cover that this way: /(?:(?!foo)...|^..?)bar/. Sometimes it's still easier just to say: if (/foo/ && $` =~ /bar$/) を読んでもじぇんじぇん理解できない俺は頭が悪いのだろうか? -- 人生を背負い投げ 菊谷 誠(Kikutani Makoto) kikutani@sprintmail.com