[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Regular Expression question

seebs

6/30/2007 2:42:00 AM

In message <0df4a451b19810ff6e06f11725324591@ruby-forum.com>, Al Cholic writes:
>Im working with regular expressions and I cant quite understand how the
>"13" is extracted from the string.
>Here is the irb output:
>irb(main):005:0> "(13)"[1..-2].to_i
>=> 13
>I dont understadn how the [1..-2] parameter removes the parenthases from
>the string.

"(13)"[0] = "("
"(13)"[1] = "1"
"(13)"[2] = "3"
"(13)"[3] = ")"

"(13)"[-1] = ")"
"(13)"[-2] = "3"
"(13)"[-3] = "1"
"(13)"[-4] = "("

"-N" counts from the end, starting with -1.

-2 is the character just before the end (the 3). 1 is the character just after
the beginning (the 1).

From "just after the beginning" to "just before the end" is the string without
the parentheses.

However, I must correct you: You are not working with regular expressions.
There are no regular expressions here, only array slices.

-s

2 Answers

Kristen

6/30/2007 2:51:00 AM

0


> "(13)"[0] = "("
> "(13)"[1] = "1"
> "(13)"[2] = "3"
> "(13)"[3] = ")"
>
> "(13)"[-1] = ")"
> "(13)"[-2] = "3"
> "(13)"[-3] = "1"
> "(13)"[-4] = "("
>
> "-N" counts from the end, starting with -1.


Thanks. An the .. in [1..-2] means "keep everything in between" ?


--
Posted via http://www.ruby-....

Robert Dober

6/30/2007 7:33:00 AM

0

On 6/30/07, Peter Seebach <seebs@seebs.net> wrote:
> In message <0df4a451b19810ff6e06f11725324591@ruby-forum.com>, Al Cholic writes:
> >Im working with regular expressions and I cant quite understand how the
> >"13" is extracted from the string.
> >Here is the irb output:
> >irb(main):005:0> "(13)"[1..-2].to_i
> >=> 13
> >I dont understadn how the [1..-2] parameter removes the parenthases from
> >the string.
>
> "(13)"[0] = "("
> "(13)"[1] = "1"
> "(13)"[2] = "3"
> "(13)"[3] = ")"
>
> "(13)"[-1] = ")"
> "(13)"[-2] = "3"
> "(13)"[-3] = "1"
> "(13)"[-4] = "("
Maybe it would not hurt to add a little clarification, albeit the fact
that your didactic simplification has worked very well :). Especially
as this concerns a FAQ

In reality
"(13)"[0] => ?( which equals 40
and
"(13)"[0..0] => "("

Cheers
Robert
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck