On May 4, 3:11 ¨Âí¬ Òïâåòô Ëìåííå ¼óèïòôãõô®®®Àçïïçìåíáéì®ãïí¾ ÷òïôåº > On 03.05.2008 18:00, lovea... / gmail.com wrote: > > > I am not sure if this is possible. I have a case statement which > > checks for a regular expression. For example: > > > case text > > when /(abc)(.)*/ > > when /(xyz)(.)*/ > > end > > > In the above snippet how can I use the instance variables pre_match > > and post_match? I tried calling these methods without any qualifiers > > but I got a NoMethodError. Where does Ruby store the MatchData if the > > matching is done in a case statement? > > $ irb > irb(main):001:0> /b/ =~ "abc" > => 1 > irb(main):002:0> $` > => "a" > irb(main):003:0> $' > => "c" > irb(main):004:0> $~ > => #<MatchData:0x7ff9eb54> > irb(main):005:0> $~.to_a > => ["b"] > irb(main):006:0> $~.pre_match > => "a" > irb(main):007:0> $~.post_match > => "c" > irb(main):008:0> > > But why do you have groups in your regular expressions if you are not > interested in the content? ¨Âì󢨮©ª¢ óååíâéïäâåãáõóéô íéçèôïùéåì÷èáô ùïåøðåãô> > irb(main):008:0> /a(.)*/ =~ "abcdef" > => 0 > irb(main):009:0> $1 > => "f" > > IMHO it is generally a bad idea to use grouping in the way you do it > because it will capture a lot that you are not interested in. ¨Âóååí> you might rather want "(.*)". > > Kind regards > > robert Hey Robert, In my guess /b/ =~ "abc" is what sets $~ variable. Since I am not using that statement anywhere that variable is not set at all. Instead I am doing the matching in a case statement. As a result I am not able to do something like $~.pre_match. I am not fond of $1, $2, etc variables. I feel comfortable using the instance variables pre_match and post_match. But you solved the first problem I was trying to solve :) I was using (.)* instead of (.*) and I couldn't get what I wanted. (.*) is what I needed. Thanks so much for that. Now I can use $1, $2 at least if not pre_match and post_match. -subbu