Forum: Ruby regexp help needed

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2007-04-28 01:51
(Received via mailing list)
Hey All,

I need to parse lines that look like this:

<lines>
1 'Not qualified' 2 'Overquota' 3 'Qualified'/
1 'SSI' 2 'Mall Facility'/
1 'Real Interview' 2 'Practice Interview'/
</lines>

So I've got N sets of <<digits>><<space>><<quote-delimited-label>> on
each line.  I want to grab each of the digits & labels, but I'm having
trouble w/the repetition stuff.  Below is a simple script that doesn't
work--it grabs the first set, but seems to ignore the rest.

str = "1 'Not qualified' 2 'Overquota' 3 'Qualified'"
rgx = /((\d+)\s+(\'[^']+?\'))+/i
m = rgx.match(str)
unless m.nil?
   m.captures.each {|c| puts(c)}
end

Can anybody throw me a regex clue here?

Thanks!

- Roy
Aaron P. (Guest)
on 2007-04-28 02:10
(Received via mailing list)
Hey Roy,
On Sat, Apr 28, 2007 at 06:50:10AM +0900, removed_email_address@domain.invalid 
wrote:
> So I've got N sets of <<digits>><<space>><<quote-delimited-label>> on
>
> Can anybody throw me a regex clue here?

You might want to try the scan method:

  str = "1 'Not qualified' 2 'Overquota' 3 'Qualified'"

  str.scan(/(\d+)\s+('[^']+')+/).each do |match|
    p match
  end

Hope that helps!
Duane J. (Guest)
on 2007-04-28 02:12
(Received via mailing list)
Perhaps a little something like this:

str = "1 'Not qualified' 2 'Overquota' 3 'Qualified'"
rgx = /((\d+)\s+(\'[^']+?\'))+/i
m = str.scan(rgx)
m.each do |match|
  puts match.first
end

Duane J.
(canadaduane)
Tim P. (Guest)
on 2007-04-28 02:15
(Received via mailing list)
On 4/27/07, removed_email_address@domain.invalid 
<removed_email_address@domain.invalid> wrote:
> So I've got N sets of <<digits>><<space>><<quote-delimited-label>> on
>
r = %r/(\d+)\s+('[^']*')/
s = "1 'Not qualified' 2 'Overquota' 3 'Qualified'"

s.scan(r)          #=> [["1", "'Not qualified'"], ["2",
"'Overquota'"], ["3", "'Qualified'"]]



Blessings,
TwP
(Guest)
on 2007-04-28 02:26
(Received via mailing list)
Ah--that's the magic!  Thanks guys!

-Roy
This topic is locked and can not be replied to.