From: Shin-ichiro HARA Date: 1997-12-16T16:31:01+09:00 Subject: [ruby-dev:1107] Re: Regexp#operators (Re: ruby 1.1b1 released) 原です。 From: matz@netlab.co.jp (Yukihiro Matsumoto) Subject: [ruby-dev:1103] Re: Regexp#operators (Re: ruby 1.1b1 released) Date: Tue, 16 Dec 97 11:02:05 +0900 > まつもと ゆきひろです > うーん,実は演算子 ~ には RE =~ $_ の省略系と言う定義が既に > あるんですね.もっとも条件式では自動的に $_ とのマッチが取ら > れますし,あまり使われていないだろうことは間違いないので,変 > 更は可だと思いますけどね. そういえばそうでした。! はメソッドじゃないから使えないし、、、。 やっぱり ~~~ か、、:-)。 > |ちなみにこの場合、string =~ /foo/ & /bar/ は、 > |string =~ /foo/ && string =~ /bar/ と結構異なります。 > > これを実現するためのeregex.rbというのを書いてみました.どう > 思います? (/foo/ | /bar/) | /baz/ とか、(/foo/ | /bar/) & /baz/ というのも許すとすれば、 class Regs def initialize(re1, re2, op) @re1 = re1 @re2 = re2 @op = op end def =~ (str) case @op when :| @re1 =~ str or @re2 =~ str when :& @re1 =~ str and @re2 =~ str end end def |(other) Regs.new(self, other, :|) end def &(other) Regs.new(self, other, :&) end end class Regexp def |(other) Regs.new(self, other, :|) end def &(other) Regs.new(self, other, :&) end end ということになりますかね。 しかし、私は str =~ /foo/ & /bar/ は、str =~ /foo/ && str =~ /bar/ と同じでなくてもいい気がするんです。「正規表現の話」なら & は && と 同値であるべきだけど、これは「正規表現の文字列への適合の話」もからん でいるから同値でなくてもよい、という解釈なわけです。