Dave_Rose
(Dave Rose)
19 January 2007 21:19
1
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?
···
--
Posted via http://www.ruby-forum.com/ .
Hope there is one solution O.K. for you.
>>>>> Code >>>>>
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----------"}
>>>>> Results >>>>>
########## 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....
----------
A.....1
c....
t....
Z.....
----------
>>>>> End >>>>>
Wolfgang Nádasi-Donner
Dave_Rose
(Dave Rose)
22 January 2007 13:49
3
Wolfgang Nádasi-donner wrote:
Hope there is one solution O.K. for you.
>>>>> Code >>>>>
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
···
--
Posted via http://www.ruby-forum.com/\ .