Forum: Ruby Regular Expression question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5b20b09d5d24bd554f703ff0595696b?d=identicon&s=25 Al Cholic (desertfox)
on 2007-06-30 04:31
Hello,

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.


Could someone please explain.

Thanks in advance.
B8cfd5ec0f88bf5b5f2eedda7d1a0746?d=identicon&s=25 unknown (Guest)
on 2007-06-30 04:42
(Received via mailing list)
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
C5b20b09d5d24bd554f703ff0595696b?d=identicon&s=25 Al Cholic (desertfox)
on 2007-06-30 04:50
> "(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" ?
B8cfd5ec0f88bf5b5f2eedda7d1a0746?d=identicon&s=25 unknown (Guest)
on 2007-06-30 04:56
(Received via mailing list)
In message <a0d519101f6199ed9f37558d7cec5351@ruby-forum.com>, Al Cholic
writes:
>Thanks.  An the .. in [1..-2] means "keep everything in between" ?

Right.

So, "hello, world!"[1..-2] => "ello, world"

-s
C5b20b09d5d24bd554f703ff0595696b?d=identicon&s=25 Al Cholic (desertfox)
on 2007-06-30 05:02
unknown wrote:
> In message <a0d519101f6199ed9f37558d7cec5351@ruby-forum.com>, Al Cholic
> writes:
>>Thanks.  An the .. in [1..-2] means "keep everything in between" ?
>
> Right.
>
> So, "hello, world!"[1..-2] => "ello, world"
>
> -s


Thank you very much.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-06-30 09:34
(Received via mailing list)
On 6/30/07, Peter Seebach <seebs@seebs.net> wrote:
> "(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
B8cfd5ec0f88bf5b5f2eedda7d1a0746?d=identicon&s=25 unknown (Guest)
on 2007-06-30 09:41
(Received via mailing list)
In message
<335e48a90706300032p287c0afepc307adabc12dfb73@mail.gmail.com>, "Robert
Dober" writes
:
>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

Oops, doh!

You are correct, of course.

-s
This topic is locked and can not be replied to.