[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Backreferences in case statements

loveajax

5/3/2008 4:01:00 PM

Hi,

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?

Thanks
-subbu
27 Answers

ara.t.howard

5/3/2008 4:15:00 PM

0


On May 3, 2008, at 10:05 AM, loveajax@gmail.com wrote:
> 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?


$1, $2, etc.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Robert Klemme

5/4/2008 10:12:00 AM

0

On 03.05.2008 18:00, loveajax@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? Also "(.)*" seems a bit odd because it might
not yield what you expect:

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. It seems
you might rather want "(.*)".

Kind regards

robert

Pit Capitain

5/4/2008 12:41:00 PM

0

2008/5/3 <loveajax@gmail.com>:
> Where does Ruby store the MatchData if the
> matching is done in a case statement?

Subbu, there's also Regexp.last_match. Sometimes this reads better
than those Perl-like variables.

Regards,
Pit

loveajax

5/4/2008 1:22:00 PM

0

On May 4, 3:11 am, Robert Klemme <shortcut...@googlemail.com> wrote:
> 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?  Also "(.)*" seems a bit odd because it might
> not yield what you expect:
>
> 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.  It seems
> 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

loveajax

5/4/2008 1:24:00 PM

0

On May 4, 5:40 am, Pit Capitain <pit.capit...@gmail.com> wrote:
> 2008/5/3  <lovea...@gmail.com>:
>
> >  Where does Ruby store the MatchData if the
> >  matching is done in a case statement?
>
> Subbu, there's also Regexp.last_match. Sometimes this reads better
> than those Perl-like variables.
>
> Regards,
> Pit

Hi Pitt,

I did use Regexp.last_match. But its a class method. I am interested
in using something like pre_match and post_match methods which are
instance variables. But looks like my code never created the instance
of MatchData.

Thanks
-subbu

James Gray

5/4/2008 6:11:00 PM

0

On May 4, 2008, at 8:25 AM, loveajax@gmail.com wrote:

> On May 4, 5:40 am, Pit Capitain <pit.capit...@gmail.com> wrote:
>> 2008/5/3 <lovea...@gmail.com>:
>>
>>> Where does Ruby store the MatchData if the
>>> matching is done in a case statement?
>>
>> Subbu, there's also Regexp.last_match. Sometimes this reads better
>> than those Perl-like variables.
>>
>> Regards,
>> Pit
>
> Hi Pitt,
>
> I did use Regexp.last_match. But its a class method. I am interested
> in using something like pre_match and post_match methods which are
> instance variables. But looks like my code never created the instance
> of MatchData.

Regexp.last_match.pre_match

James Edward Gray II


Robert Klemme

5/4/2008 7:05:00 PM

0

On 04.05.2008 15:21, loveajax@gmail.com wrote:
> On May 4, 3:11 am, Robert Klemme <shortcut...@googlemail.com> wrote:
>> 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? Also "(.)*" seems a bit odd because it might
>> not yield what you expect:
>>
>> 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. It seems
>> you might rather want "(.*)".

> In my guess /b/ =~ "abc" is what sets $~ variable. Since I am not
> using that statement anywhere that variable is not set at all.

That's complete nonsense. Did you try it out? I guess not. Come on,
don't be so lazy.

> 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.

pre_match and post_match are not instance variables but methods of class
MatchData.

> 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.

irb(main):007:0> /b/ === "abc"
=> true
irb(main):008:0> $~
=> #<MatchData:0x100230b8>
irb(main):009:0> $~.pre_match
=> "a"
irb(main):010:0> $~.post_match
=> "c"
irb(main):011:0>

robert

Rick DeNatale

5/5/2008 12:48:00 AM

0

On Sun, May 4, 2008 at 11:22 AM, David A. Black <dblack@rubypal.com> wrote:

> > > 2008/5/3 <lovea...@gmail.com>:
> > > > Where does Ruby store the MatchData if the
> > > > matching is done in a case statement?
>
> Matching doesn't change in or out of a case statement; it's still the
> same thing.

> Ruby keeps track of the last match operation performed, and that's
> what you get in $~ (which is a MatchData object) and some of the
> Regexp class methods.

And $~ is actually a thread local global, so it holds the matchdata
produced by the last successful match on the current thread.

I'm pretty sure that Regexp.last_match is thread-safe as well.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

David A. Black

5/5/2008 5:21:00 AM

0

Hi --

On Mon, 5 May 2008, Rick DeNatale wrote:

> On Sun, May 4, 2008 at 11:22 AM, David A. Black <dblack@rubypal.com> wrote:
>
>>>> 2008/5/3 <lovea...@gmail.com>:
>>>>> Where does Ruby store the MatchData if the
>>>>> matching is done in a case statement?
>>
>> Matching doesn't change in or out of a case statement; it's still the
>> same thing.
>
>> Ruby keeps track of the last match operation performed, and that's
>> what you get in $~ (which is a MatchData object) and some of the
>> Regexp class methods.
>
> And $~ is actually a thread local global, so it holds the matchdata
> produced by the last successful match on the current thread.
>
> I'm pretty sure that Regexp.last_match is thread-safe as well.

I believe it just returns $~, so it should be identical in terms of
thread safety.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Fly-n-G

7/1/2012 10:52:00 PM

0

Milt wrote:
> On Jun 29, 12:57 pm, Fly-n-G <yarbe...@dr.oog> wrote:
>> Milt wrote:
>>> On Jun 28, 3:25 pm, Fly-n-G <yarbe...@dr.oog> wrote:
>>>> 3026 Dead wrote:
>>>>> On Thu, 28 Jun 2012 08:59:00 -0600, Fly-n-G wrote:
>>
>>>>>> 3026 Dead wrote:
>>>>>>> On Wed, 27 Jun 2012 22:18:28 -0600, Fly-n-G wrote:
>>
>>>>>>>> 3026 Dead wrote:
>>
>>>>>>>>> Could you define what loan-sharking it for us, please?
>>
>>>>>>>> The practice of lending money at usurious, often illegal interest
>>>>>>>> rates.
>>
>>>>>>> VERY good. And since there is no legal maximum on
>>
>>>>>> Read and LEARN they same thing the MORON needs to:
>>
>>>>>> loan sharking
>>
>>>>>> Charging an illegally high interest rate on a loan. also called usury.
>>
>>>>>> Read more:
>>>>>> http://www.investorwords.com/5639/loan_sharking.html#ixz...
>>
>>>>> but
>>
>>>> STFU tubby, you have NOTHING to say here.
>>
>>> The Stalker needed to create another identity for THIS? How pathetic.
>>
>> Nope, your paranoia is off the leash again, lol.
>>
>> The guy is knee deep in your thick skull, isn't he?
>
> I'm in HIS head?

No, he's sure playing games inside of yours.

> He's the one creating sock puppets.

Nah, that's just your paranoia talking.

> He's the one who
> can't seem to resist responding to my posts.

Yet here YOU are trying to get the last word again...

>>
>> I just enjoy whipping you into a lather for him now and again, it's
>> amusenet at it's best, lol.
>>
>> And with your regular penchant for shooting your mouth off on subjects
>> you have no clue about you make an easy target.
>
> Now THAT's ironic, if you know anything about the Stalker.
>
> And we both know you do.

Oh most definitely, he's got an archive on you that makes your head spin.

Too funny.

>>
>> enjoy!
>