Regexp.newbie?

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?

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs