Regular Expression question

A

Al Cholic

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

Al Cholic

"(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" ?
 
R

Robert Dober

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,977
Latest member
MillaDowdy

Latest Threads

Top