# Regular Expression question

**URL:** <https://www.ruby-forum.com/t/regular-expression-question/108214>\
**Category:** Ruby\
**Created:** [June 30, 2007, 4:31am UTC](https://www.ruby-forum.com/t/regular-expression-question/108214 "2007-06-30T04:31:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [June 30, 2007, 4:31am UTC](https://www.ruby-forum.com/t/regular-expression-question/108214/1 "2007-06-30T04:31:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [June 30, 2007, 4:42am UTC](https://www.ruby-forum.com/t/regular-expression-question/108214/2 "2007-06-30T04:42:47Z")

</div>

In message [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), 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

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [June 30, 2007, 4:50am UTC](https://www.ruby-forum.com/t/regular-expression-question/108214/3 "2007-06-30T04:50:42Z")

</div>

> “(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” ?

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [June 30, 2007, 4:56am UTC](https://www.ruby-forum.com/t/regular-expression-question/108214/4 "2007-06-30T04:56:48Z")

</div>

In message [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), Al Cholic  
writes:

> Thanks. An the … in [1…-2] means “keep everything in between” ?

Right.

So, “hello, world!”[1…-2] =\> “ello, world”

-s

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [June 30, 2007, 5:02am UTC](https://www.ruby-forum.com/t/regular-expression-question/108214/5 "2007-06-30T05:02:03Z")

</div>

unknown wrote:

> In message [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), 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.

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [June 30, 2007, 9:41am UTC](https://www.ruby-forum.com/t/regular-expression-question/108214/6 "2007-06-30T09:41:04Z")

</div>

In message  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), “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

---

<div class="post-metadata">

**Author:** ![desertfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@desertfox](https://www.ruby-forum.com/u/desertfox)\
**Post date:** [June 30, 2007, 9:34am UTC](https://www.ruby-forum.com/t/regular-expression-question/108214/7 "2007-06-30T09:34:04Z")

</div>

On 6/30/07, Peter S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
