regexp help needed

R

rpardee

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
 
A

Aaron Patterson

Hey Roy,
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!
 
D

Duane Johnson

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)

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
 
T

Tim Pease

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
 
R

rpardee

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

-Roy

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
 

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

Forum statistics

Threads
474,241
Messages
2,571,219
Members
47,850
Latest member
StewartTha

Latest Threads

Top