# Too greedy of a regexp

**URL:** <https://www.ruby-forum.com/t/too-greedy-of-a-regexp/75396>\
**Category:** Ruby\
**Created:** [November 9, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/too-greedy-of-a-regexp/75396 "2006-11-09T17:35:25Z")\
**Posts on this page:** 4\
**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:** [November 9, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/too-greedy-of-a-regexp/75396/1 "2006-11-09T17:35:25Z")

</div>

i have a regexp: /(^BillHead(._))(^Bill\_End(._))/m that’s too greedy for  
processing  
a billing extract file containing:  
BillHead…\<\<\>\n  
\<\<one or more detail lines here\n\>\>  
Bill\_End…\<\<\>\n  
BillHead…\<\<\>\n  
\<\<one or more detail lines here\n\>\>  
Bill\_End…\<\<\>\n  
…etc… to EOF…

…i get the whole file matched…i just want each invoice…  
it will eventually be in a oneliner like  
a=File.read(“billfile”).scan(regexp)

so what is the non-greedy way for the above regexp to properly match  
each invoice…

---

<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:** [November 9, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/too-greedy-of-a-regexp/75396/2 "2006-11-09T18:21:32Z")

</div>

On 11/9/06, Dave R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …i get the whole file matched…i just want each invoice…  
> it will eventually be in a oneliner like  
> a=File.read(“billfile”).scan(regexp)
> 
> so what is the non-greedy way for the above regexp to properly match  
> each invoice…

try:

/(^BillHead(._?))(^Bill\_End(._?))\n/m

or

/(^BillHead(._?))(^Bill\_End([^\n]._))\n/m

notice the ._? instead of ._

.\*? has some pecularities, that were discussed here some time ago, so  
perhaps you’d want to find them in the archives. (search for ‘greedy’  
or ‘regex’ - I don’t remeber now)

---

<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:** [November 9, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/too-greedy-of-a-regexp/75396/3 "2006-11-09T19:23:55Z")

</div>

Jan S. wrote:

> > …etc… to EOF…  
> > /(^BillHead(._?))(^Bill\_End(._?))\n/m
> 
> or
> 
> /(^BillHead(._?))(^Bill\_End([^\n]._))\n/m
> 
> notice the ._? instead of ._
> 
> \*? has some pecularities, that were discussed here some time ago, so  
> perhaps you’d want to find them in the archives. (search for ‘greedy’  
> or ‘regex’ - I don’t remeber now)

I would also remove the last .\* because that likely eats up the rest of  
the document. So that would be

/^BillHead(.\*?)^BillEnd/m

Another approach is to do

s.split(/^(Bill(?:Head|End))/m)

and then go through the array.

irb(main):006:0\>  
“BillHead\nfoo\nbar\nBillEnd”.split(/^(Bill(?:Head|End))/m)  
=\> ["", “BillHead”, “\nfoo\nbar\n”, “BillEnd”]

Kind regards

```
robert
```

---

<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:** [November 9, 2006, 8:04pm UTC](https://www.ruby-forum.com/t/too-greedy-of-a-regexp/75396/4 "2006-11-09T20:04:54Z")

</div>

Robert K. wrote:

> Jan S. wrote:
> 
> > > …etc… to EOF…  
> > > /(^BillHead(._?))(^Bill\_End(._?))\n/m
> > 
> > or
> > 
> > /(^BillHead(._?))(^Bill\_End([^\n]._))\n/m
> > 
> > notice the ._? instead of ._

=\> ["", “BillHead”, “\nfoo\nbar\n”, “BillEnd”]

> Kind regards
> 
> robert  
> i played around in irb with a shorten extract file and found that:  
> b=File.read(“drbilp.txt”).scan(/(^BillHead(._?))(^Bill\_End(\d_)(\s_UBPBILP1\n)(._?))/m)  
> works in that separates each invoice in an sub-array of size=6  
> in which b[x][0]+b[x][2] completes that task of reading,scanning  
> correctly  
> and puting all in a ruby ‘container’ that i can do an each on…thanx  
> dave
