Regexp.newbie?

D

Dave Rose

i've got a whole file to scan...but can't get neatly get the last
occurance with...
.scan(/(^A.*?)^A/m){|a| stops<<$1} ...yes i know that last occurance
is not scanned
because it is succeeded with ^Z line....that's the complication....
A......
M.....
d....
A....
r....
f....
A....
.... etc to the last A group....
A.....
c....
t....
Z.....

note each ^A group can end only with a new occurance of another A
record or the Z record.which
is the last record of the file....So? how do make a more eligant Reg
expression to cover this
situation?
 
W

Wolfgang Nádasi-Donner

Hope there is one solution O.K. for you.

mylines = <<EOT
A......1
M.....
d....
A....1
r....
f....
A.....1
c....
t....
Z.....
EOT
puts '########## Without last Z-Record ##########'
mylines.scan(/^A.*?(?=^(?:A|Z))/m){|a|puts "#{a}\n----------"}
puts '########## Last Record up to EOF ##########'
mylines.scan(/^A.*?(?=^A|\Z)/m){|a|puts "#{a}\n----------"}
puts '########## Includings last Z-Record ##########'
mylines.scan(/^A.*?(?:(?=^A)|^Z.*$)/m){|a|puts "#{a}\n----------"}

########## Without last Z-Record ##########
A......1
M.....
d....

----------
A....1
r....
f....

----------
A.....1
c....
t....

----------
########## Last Record up to EOF ##########
A......1
M.....
d....

----------
A....1
r....
f....

----------
A.....1
c....
t....
Z.....
----------
########## Includings last Z-Record ##########
A......1
M.....
d....

----------
A....1
r....
f....
 
D

Dave Rose

Wolfgang said:
Hope there is one solution O.K. for you.


mylines = <<EOT
A......1
M.....
d....
A....1
r....
f....
A.....1
c....
t....
Z.....
EOT
puts '########## Without last Z-Record ##########'
mylines.scan(/^A.*?(?=^(?:A|Z))/m){|a|puts "#{a}\n----------"}
puts '########## Last Record up to EOF ##########'
mylines.scan(/^A.*?(?=^A|\Z)/m){|a|puts "#{a}\n----------"}
puts '########## Includings last Z-Record ##########'
mylines.scan(/^A.*?(?:(?=^A)|^Z.*$)/m){|a|puts "#{a}\n----------"}



...wow...thanx..a whole lot..dave
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top