# How to improve a Generator to be more memory efficient when it is collected?

**URL:** https://discourse.julialang.org/t/how-to-improve-a-generator-to-be-more-memory-efficient-when-it-is-collected/92932
**Category:** Performance
**Tags:** question, regex, optimization, iterators
**Created:** [January 13, 2023, 6:10pm UTC](https://discourse.julialang.org/t/how-to-improve-a-generator-to-be-more-memory-efficient-when-it-is-collected/92932 "2023-01-13T18:10:11Z")
**Posts on this page:** 1
**Showing post:** 8

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [January 15, 2023, 9:52pm UTC](https://discourse.julialang.org/t/how-to-improve-a-generator-to-be-more-memory-efficient-when-it-is-collected/92932/8 "2023-01-15T21:52:27Z")

</div>

> [@Dan](#):
>
> `"ATG(?:[AGTC][AGTC][AGTC])*?(TAG|TAA|TGA)"`

`ATG` at start looks for START codon, then `(?: )*?` repeats whatever is inside 0 or more times, but lazily (that’s the `?` after `*`). This skips codons until the first (b/c lazy) of the next group. The next group is 3 string options covering the end codons. The `[AGTC]` repeating three times matches a codon, as each square bracket matches a single letter from inside it.

So actually, it could be even simpler:  
`r"ATG(?:[AGTC]{3})*?T(AG|AA|GA)"`

This link gives all the common and uncommon regular expression syntax:  
[https://www.pcre.org/current/doc/html/pcre2syntax.html](https://www.pcre.org/current/doc/html/pcre2syntax.html)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-improve-a-generator-to-be-more-memory-efficient-when-it-is-collected/92932)._
