# Positive lookahead in multiline strings (was gsub)

**URL:** <https://www.ruby-forum.com/t/positive-lookahead-in-multiline-strings-was-gsub/100134>\
**Category:** Ruby\
**Created:** [July 27, 2006, 10:43am UTC](https://www.ruby-forum.com/t/positive-lookahead-in-multiline-strings-was-gsub/100134 "2006-07-27T10:43:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [July 27, 2006, 10:43am UTC](https://www.ruby-forum.com/t/positive-lookahead-in-multiline-strings-was-gsub/100134/1 "2006-07-27T10:43:40Z")

</div>

I was just rolling my own little thread about gsub ;]  
and trying to fix my stupid regexen when  
I came up with the perfect solution to replace the last occurence of a  
pattern (if it is simple, thus not of any interest to the OP of the OT).

Consider this

string.reverse.sub("\<","").reverse is fast  
but

string.sub(Regexp.new("\<(?=[^\<]\*$)", Regexp::MULTILINE), “”)

is almost three times as fast (to my surprise), only that it does not  
work  
if there are newlines in the string, they match $.  
Is there a way to anchor on the end of the string in multiline strings.

Just didn’t find it.

Ty in advance

Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [July 27, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/positive-lookahead-in-multiline-strings-was-gsub/100134/2 "2006-07-27T13:08:12Z")

</div>

Robert D. schrieb:

> I came up with the perfect solution to replace the last occurence of a  
> pattern (if it is simple, thus not of any interest to the OP of the OT).  
> (…)  
> Is there a way to anchor on the end of the string in multiline strings.

Robert, that’s \z. See for example

[http://phrogz.net/ProgrammingRuby/language.html#regularexpressionpatterns](http://phrogz.net/ProgrammingRuby/language.html#regularexpressionpatterns)

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [July 27, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/positive-lookahead-in-multiline-strings-was-gsub/100134/3 "2006-07-27T13:31:54Z")

</div>

On 7/27/06, Pit C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Regards,  
> Pit

Thanks I _should_ have found it, but of course now reverse.sub.reverse  
is  
much faster again !!!  
Nevertheless it behaves correctly and reasonably fast.  
Thanks again.

Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein
