Regexp Capture Access

  • Thread starter Oliver Saunders
  • Start date
O

Oliver Saunders

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,"?
 
M

Mateusz Tybura

[Note: parts of this message were removed to make it a legal post.]

Try:

%r{(\w,)+}.match('a,b,c').to_s[0..1] #=> "a,"

2008/4/2 said:
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,"?
 
R

Robert Dober

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,"?
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
 
M

mutahhir hayat

[Note: parts of this message were removed to make it a legal post.]

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.


Try:

%r{(\w,)+}.match('a,b,c').to_s[0..1] #=> "a,"

2008/4/2 said:
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,"?
 
T

Todd Benson

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
 
T

Todd Benson

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
 
O

Oliver Saunders

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

Todd Benson

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
 

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

Members online

No members online now.

Forum statistics

Threads
474,289
Messages
2,571,435
Members
48,121
Latest member
ColinHibne

Latest Threads

Top