# Regexp.newbie?

**URL:** <https://www.ruby-forum.com/t/regexp-newbie/82709>\
**Category:** Ruby\
**Created:** [January 19, 2007, 10:19pm UTC](https://www.ruby-forum.com/t/regexp-newbie/82709 "2007-01-19T22:19:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [January 19, 2007, 10:19pm UTC](https://www.ruby-forum.com/t/regexp-newbie/82709/1 "2007-01-19T22:19:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [January 19, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/regexp-newbie/82709/2 "2007-01-19T22:46:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [January 22, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/regexp-newbie/82709/3 "2007-01-22T14:49:47Z")

</div>

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
