[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

regexp help needed

rpardee@gmail.com

4/27/2007 9:47:00 PM

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

4 Answers

Aaron Patterson

4/27/2007 10:09:00 PM

0

Hey Roy,
On Sat, Apr 28, 2007 at 06:50:10AM +0900, rpardee@gmail.com wrote:
> 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?

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!

--
Aaron Patterson
http://tenderlovem...

Duane Johnson

4/27/2007 10:12:00 PM

0

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 Johnson
(canadaduane)

On 4/27/07, rpardee@gmail.com <rpardee@gmail.com> wrote:
> 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
>
>
>


--
Duane Johnson
(canadaduane)

Tim Pease

4/27/2007 10:15:00 PM

0

On 4/27/07, rpardee@gmail.com <rpardee@gmail.com> wrote:
> 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
>


r = %r/(\d+)\s+('[^']*')/
s = "1 'Not qualified' 2 'Overquota' 3 'Qualified'"

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



Blessings,
TwP

rpardee@gmail.com

4/27/2007 10:22:00 PM

0

Ah--that's the magic! Thanks guys!

-Roy

On Apr 27, 3:15 pm, "Tim Pease" <tim.pe...@gmail.com> wrote:
> On 4/27/07, rpar...@gmail.com <rpar...@gmail.com> wrote:
>
>
>
> > 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
>
> r = %r/(\d+)\s+('[^']*')/
> s = "1 'Not qualified' 2 'Overquota' 3 'Qualified'"
>
> s.scan(r) #=> [["1", "'Not qualified'"], ["2",
> "'Overquota'"], ["3", "'Qualified'"]]
>
> Blessings,
> TwP