Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regexp multiple matches

Reply
Thread Tools

regexp multiple matches

 
 
Mickael Faivre-Macon
Guest
Posts: n/a
 
      01-28-2009
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-forum.com/.

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      01-28-2009
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.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

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

 
Reply With Quote
 
 
 
 
Andrew Timberlake
Guest
Posts: n/a
 
      01-28-2009
[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 <(E-Mail Removed)>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-forum.com/.
>
>

Mickael

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

--
Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

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

 
Reply With Quote
 
Mickael Faivre-Macon
Guest
Posts: n/a
 
      01-28-2009
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
> <(E-Mail Removed)>wrote:
>
>> 5: (2)
>>
>> My question is why ;5,7 is not matched ?
>>
>> Thanks,
>> Mickael.
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>

> Mickael
>
> Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;3,2
> which
> matches based on the '*'
>
> --
> Andrew Timberlake
> http://ramblingsonrails.com
> http://www.linkedin.com/in/andrewtimberlake
>
> "I have never let my schooling interfere with my education" - Mark Twain


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

 
Reply With Quote
 
Andrew Timberlake
Guest
Posts: n/a
 
      01-28-2009
[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 <(E-Mail Removed)>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
> > <(E-Mail Removed)>wrote:
> >
> >> 5: (2)
> >>
> >> My question is why ;5,7 is not matched ?
> >>
> >> Thanks,
> >> Mickael.
> >> --
> >> Posted via http://www.ruby-forum.com/.
> >>
> >>

> > Mickael
> >
> > Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;3,2
> > which
> > matches based on the '*'
> >
> > --
> > Andrew Timberlake
> > http://ramblingsonrails.com
> > http://www.linkedin.com/in/andrewtimberlake
> >
> > "I have never let my schooling interfere with my education" - Mark Twain

>
> --
> Posted via http://www.ruby-forum.com/.
>
>

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://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

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

 
Reply With Quote
 
Mickael Faivre-Macon
Guest
Posts: n/a
 
      01-28-2009
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
> <(E-Mail Removed)>wrote:
>
>> >> My question is why ;5,7 is not matched ?
>> > which

>> Posted via http://www.ruby-forum.com/.
>>
>>

> 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://ramblingsonrails.com
> http://www.linkedin.com/in/andrewtimberlake
>
> "I have never let my schooling interfere with my education" - Mark Twain


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

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      01-28-2009

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
>> <(E-Mail Removed)>wrote:
>>
>>> 5: (2)
>>>
>>> My question is why ;5,7 is not matched ?
>>>
>>> Thanks,
>>> Mickael.
>>> --
>>> Posted via http://www.ruby-forum.com/.
>>>
>>>

>> 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://ramblingsonrails.com
>> http://www.linkedin.com/in/andrewtimberlake
>>
>> "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://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
replace random matches of regexp gry Python 4 09-08-2011 09:19 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
[Regexp] Howto capture all matches of a single group ersin.er@gmail.com Java 3 10-03-2005 10:11 AM
multiple regexp matches Kevin Howe Ruby 27 08-24-2004 02:30 PM
Please help with regexp - finding all matches? Boris Pelakh Perl 3 04-08-2004 08:14 PM



Advertisments