# Regexp Capture Access

**URL:** <https://www.ruby-forum.com/t/regexp-capture-access/133679>\
**Category:** Ruby\
**Created:** [April 2, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/regexp-capture-access/133679 "2008-04-02T15:20:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [April 2, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/regexp-capture-access/133679/1 "2008-04-02T15:20:11Z")

</div>

Given that:

%r{(\w,)+}.match(‘a,b,c’)[0] #=\> “a,b,”

and

%r{(\w,)+}.match(‘a,b,c’)[1] #=\> “b,”

How do I access the capture that contains “a,”?

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [April 2, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/regexp-capture-access/133679/2 "2008-04-02T16:05:18Z")

</div>

Try:

%r{(\w,)+}.match(‘a,b,c’).to\_s[0…1] #=\> “a,”

2008/4/2, Oliver S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [April 2, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/regexp-capture-access/133679/3 "2008-04-02T16:28:35Z")

</div>

On Wed, Apr 2, 2008 at 3:20 PM, Oliver S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Use scan

“a,b,c”.scan(/\w+,/) → [‘a,’, ‘b,’]  
and in order to have the whole match you have to join the result of scan  
again.

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [April 2, 2008, 4:32pm UTC](https://www.ruby-forum.com/t/regexp-capture-access/133679/4 "2008-04-02T16:32:08Z")

</div>

The reason why it’s acting like this is because Regexes try to find the  
largest possible match from your expression. Hence, your expression

%r{(\w,)+}.match(‘a,b,c’)[0]

will return the first match (and largest) “a,b,”

Im sure you’re asking this for a bigger reason, so you have to refine  
your  
regular expression for that specific purpose. if you just want to split  
based on a “,” then simply do ‘a,b,c’.split(’,’) which returns an array  
[“a”, “b”, “c”]

If any other, please post the actual problem.

HTH  
Mutahhir.

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [April 2, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/regexp-capture-access/133679/5 "2008-04-02T16:33:19Z")

</div>

On Wed, Apr 2, 2008 at 8:20 AM, Oliver S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Given that:
> 
> %r{(\w,)+}.match(‘a,b,c’)[0] #=\> “a,b,”
> 
> and
> 
> %r{(\w,)+}.match(‘a,b,c’)[1] #=\> “b,”
> 
> How do I access the capture that contains “a,”?

Maybe leave the plus symbol (+) out?

r = /(\w,)/  
r.match(‘hi,a,b,c’)[1]  
=\> “i,”

I’d probably use #scan, or even #split, instead.

Todd

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [April 2, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/regexp-capture-access/133679/6 "2008-04-02T17:28:25Z")

</div>

Nevermind. I was trying to parse CSV. I thought pre-written tools  
weren’t capable of understanding quoted values with commas inside but I  
was wrong so I can use them.

I do find it amazing that you can’t access multiple matches for a single  
sub-pattern though. That’s a serious limitation IMO.

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [April 2, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/regexp-capture-access/133679/7 "2008-04-02T16:40:30Z")

</div>

On Wed, Apr 2, 2008 at 9:27 AM, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Use scan
> 
> “a,b,c”.scan(/\w+,/) → [‘a,’, ‘b,’]  
> and in order to have the whole match you have to join the result of scan again.
> 
> HTH  
> Robert

Yeah, I was thinking of that regexp also, but I assumed the OP wanted  
the last word letter before a comma.

Oliver, what is it that you’re looking for?

Todd

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [April 2, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/regexp-capture-access/133679/8 "2008-04-02T17:46:03Z")

</div>

On Wed, Apr 2, 2008 at 10:28 AM, Oliver S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Nevermind. I was trying to parse CSV. I thought pre-written tools  
> weren’t capable of understanding quoted values with commas inside but I  
> was wrong so I can use them.
> 
> I do find it amazing that you can’t access multiple matches for a single  
> sub-pattern though. That’s a serious limitation IMO.

I’m not sure I understand this correctly, because I think your pattern  
was wrong.

Todd
