[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regexp multiple matches

Mickael Faivre-Macon

1/28/2009 11:27:00 AM

Hi,

/(\d+),(\d+)(;(\d+),(\d+))*/

This regexp matches are:
0: (5,1;5,7;3,2)
1: (5)
2: (1)
3: (;3,2)
4: (3)
5: (2)

My question is why ;5,7 is not matched ?

Thanks,
Mickael.
--
Posted via http://www.ruby-....

6 Answers

David A. Black

1/28/2009 12:05:00 PM

0

Hi --

On Wed, 28 Jan 2009, Mickael Faivre-Macon wrote:

> Hi,
>
> /(\d+),(\d+)(;(\d+),(\d+))*/
>
> This regexp matches are:
> 0: (5,1;5,7;3,2)
> 1: (5)
> 2: (1)
> 3: (;3,2)
> 4: (3)
> 5: (2)
>
> My question is why ;5,7 is not matched ?

What's the string?


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Andrew Timberlake

1/28/2009 12:27:00 PM

0

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

On Wed, Jan 28, 2009 at 1:26 PM, Mickael Faivre-Macon <faivrem@gmail.com>wrote:

> Hi,
>
> /(\d+),(\d+)(;(\d+),(\d+))*/
>
> This regexp matches are:
> 0: (5,1;5,7;3,2)
> 1: (5)
> 2: (1)
> 3: (;3,2)
> 4: (3)
> 5: (2)
>
> My question is why ;5,7 is not matched ?
>
> Thanks,
> Mickael.
> --
> Posted via http://www.ruby-....
>
>
Mickael

Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;3,2 which
matches based on the '*'

--
Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

"I have never let my schooling interfere with my education" - Mark Twain

Mickael Faivre-Macon

1/28/2009 3:01:00 PM

0

And is there a way to keep previous matching ?
Mickael.


Andrew Timberlake wrote:
> On Wed, Jan 28, 2009 at 1:26 PM, Mickael Faivre-Macon
> <faivrem@gmail.com>wrote:
>
>> 5: (2)
>>
>> My question is why ;5,7 is not matched ?
>>
>> Thanks,
>> Mickael.
>> --
>> Posted via http://www.ruby-....
>>
>>
> Mickael
>
> Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;3,2
> which
> matches based on the '*'
>
> --
> Andrew Timberlake
> http://ramblingso...
> http://www.linkedin.com/in/andrew...
>
> "I have never let my schooling interfere with my education" - Mark Twain

--
Posted via http://www.ruby-....

Andrew Timberlake

1/28/2009 4:18:00 PM

0

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

On Wed, Jan 28, 2009 at 5:00 PM, Mickael Faivre-Macon <faivrem@gmail.com>wrote:

> And is there a way to keep previous matching ?
> Mickael.
>
>
> Andrew Timberlake wrote:
> > On Wed, Jan 28, 2009 at 1:26 PM, Mickael Faivre-Macon
> > <faivrem@gmail.com>wrote:
> >
> >> 5: (2)
> >>
> >> My question is why ;5,7 is not matched ?
> >>
> >> Thanks,
> >> Mickael.
> >> --
> >> Posted via http://www.ruby-....
> >>
> >>
> > Mickael
> >
> > Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;3,2
> > which
> > matches based on the '*'
> >
> > --
> > Andrew Timberlake
> > http://ramblingso...
> > http://www.linkedin.com/in/andrew...
> >
> > "I have never let my schooling interfere with my education" - Mark Twain
>
> --
> Posted via http://www.ruby-....
>
>
What about tackling it from a completely different angle

s = "5,1;5,7;3,2"
pairs = s.split(';') #=> ["5,1", "5,7", "3,2"]
pairs.each do |pair|
pair.split(',') #=> ["5", "1"] etc
end

--
Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

"I have never let my schooling interfere with my education" - Mark Twain

Mickael Faivre-Macon

1/28/2009 4:24:00 PM

0

Sure, that's what I've just done :)
Just wondering.
Thanks anyway.

Mickael.

Andrew Timberlake wrote:
> On Wed, Jan 28, 2009 at 5:00 PM, Mickael Faivre-Macon
> <faivrem@gmail.com>wrote:
>
>> >> My question is why ;5,7 is not matched ?
>> > which
>> Posted via http://www.ruby-....
>>
>>
> What about tackling it from a completely different angle
>
> s = "5,1;5,7;3,2"
> pairs = s.split(';') #=> ["5,1", "5,7", "3,2"]
> pairs.each do |pair|
> pair.split(',') #=> ["5", "1"] etc
> end
>
> --
> Andrew Timberlake
> http://ramblingso...
> http://www.linkedin.com/in/andrew...
>
> "I have never let my schooling interfere with my education" - Mark Twain

--
Posted via http://www.ruby-....

Rob Biedenharn

1/28/2009 4:54:00 PM

0


On Jan 28, 2009, at 10:00 AM, Mickael Faivre-Macon wrote:

> Andrew Timberlake wrote:
>> On Wed, Jan 28, 2009 at 1:26 PM, Mickael Faivre-Macon
>> <faivrem@gmail.com>wrote:
>>
>>> 5: (2)
>>>
>>> My question is why ;5,7 is not matched ?
>>>
>>> Thanks,
>>> Mickael.
>>> --
>>> Posted via http://www.ruby-....
>>>
>>>
>> Mickael
>>
>> Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;
>> 3,2
>> which
>> matches based on the '*'
>>
>>
>>
>> What about tackling it from a completely different angle
>>
>> s = "5,1;5,7;3,2"
>> pairs = s.split(';') #=> ["5,1", "5,7", "3,2"]
>> pairs.each do |pair|
>> pair.split(',') #=> ["5", "1"] etc
>> end
>>
>> --
>> Andrew Timberlake
>> http://ramblingso...
>> http://www.linkedin.com/in/andrew...
>>
>> "I have never let my schooling interfere with my education" - Mark
>> Twain
>>
>
> And is there a way to keep previous matching ?
> Mickael.

>
> --

irb> re = /(\d+),(\d+)(;(\d+),(\d+))*/
=> /(\d+),(\d+)(;(\d+),(\d+))*/
irb> s = "5,1;5,7;3,2"
=> "5,1;5,7;3,2"
irb> s.match(re).captures
=> ["5", "1", ";3,2", "3", "2"]

You can wrap another set of parentheses in there:

irb> re = /(\d+),(\d+)((;(\d+),(\d+))*)/
=> /(\d+),(\d+)((;(\d+),(\d+))*)/
irb> s.match(re).captures
=> ["5", "1", ";5,7;3,2", ";3,2", "3", "2"]

But in addition to Andrew's split, you could use String#scan

irb> s.scan(/(\d+),(\d+)/)
=> [["5", "1"], ["5", "7"], ["3", "2"]]

It all depends on what you're trying to accomplish.

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com