# Replace a string delimited by 2 other string, regexp problem

**URL:** <https://www.ruby-forum.com/t/replace-a-string-delimited-by-2-other-string-regexp-problem/70781>\
**Category:** Ruby\
**Created:** [October 2, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/replace-a-string-delimited-by-2-other-string-regexp-problem/70781 "2006-10-02T21:55:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSSSbastien\_Mauret](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f17d59/32.png) [@SSSSSbastien\_Mauret](https://www.ruby-forum.com/u/SSSSSbastien_Mauret)\
**Post date:** [October 2, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/replace-a-string-delimited-by-2-other-string-regexp-problem/70781/1 "2006-10-02T21:55:55Z")

</div>

Hello,

I want to replace a string in a file who is delimited by 2 other string.  
My problem is to find the start and the end string delimiter and replace  
the content between both.

Ex:  
str = " /\* startdel1 \*/ text1 text1 text1 /_enddel1_/ /_startdel2_/  
text2 text2 text2 /_enddel2_/ /_startdel3_/ text3 text3 text3  
/_enddel3_/ "

In this example i’want to replace the string /_startdel2_/ text2 text2  
text2 /_enddel2_/  
by

/_startdel2_/ hello /_enddel2_/

i’m newbie and it’s a real problem for me to do that.  
help me  
thx

* * *

Yahoo! Mail réinvente le mail ! Découvrez le nouveau Yahoo! Mail et son  
interface révolutionnaire.

> **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
>
> It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![SSSSSbastien\_Mauret](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f17d59/32.png) [@SSSSSbastien\_Mauret](https://www.ruby-forum.com/u/SSSSSbastien_Mauret)\
**Post date:** [October 2, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/replace-a-string-delimited-by-2-other-string-regexp-problem/70781/2 "2006-10-02T22:15:57Z")

</div>

On Tue, Oct 03, 2006 at 04:55:44AM +0900, S?bastien Maurette wrote:

> Hello,
> 
> I want to replace a string in a file who is delimited by 2 other string.  
> My problem is to find the start and the end string delimiter and replace  
> the content between both.

Learn about regular expressions, and then look into the gsub method of  
String. You can get a very brief intro to regular expressions here:  
[http://ruby-doc.org/docs/ProgrammingRuby/html/language.html#UJ](http://ruby-doc.org/docs/ProgrammingRuby/html/language.html#UJ)

---

<div class="post-metadata">

**Author:** ![SSSSSbastien\_Mauret](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f17d59/32.png) [@SSSSSbastien\_Mauret](https://www.ruby-forum.com/u/SSSSSbastien_Mauret)\
**Post date:** [October 2, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/replace-a-string-delimited-by-2-other-string-regexp-problem/70781/3 "2006-10-02T22:19:58Z")

</div>

Hello !

> Ex:  
> str = " /\* startdel1 \*/ text1 text1 text1 /_enddel1_/ /_startdel2_/  
> text2 text2 text2 /_enddel2_/ /_startdel3_/ text3 text3 text3  
> /_enddel3_/ "
> 
> In this example i’want to replace the string /_startdel2_/ text2 text2  
> text2 /_enddel2_/  
> by
> 
> /_startdel2_/ hello /_enddel2_/

left = “/_startdel2_/”  
right = “/_enddel2_/”  
p str.gsub(/#{Regexp.quote(left)}(.\*?)#{Regexp.quote(right)}/m,  
“#{left}hello#{right}”)

You need the /m as there might be newlines in the match, and you need  
the Regexp.quote as your strings contain characters which lose their  
usual meaning in a regexp (\*).

Cheers !

```
Vince
```

---

<div class="post-metadata">

**Author:** ![SSSSSbastien\_Mauret](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f17d59/32.png) [@SSSSSbastien\_Mauret](https://www.ruby-forum.com/u/SSSSSbastien_Mauret)\
**Post date:** [October 2, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/replace-a-string-delimited-by-2-other-string-regexp-problem/70781/4 "2006-10-02T23:42:13Z")

</div>

Vincent F. wrote:

> left = “/_startdel2_/”  
> right = “/_enddel2_/”  
> p str.gsub(/#{Regexp.quote(left)}(.\*?)#{Regexp.quote(right)}/m,  
> “#{left}hello#{right}”)

The “right” could be in a positive lookahead making things probably a  
likkle bit faster. No lookbehinds until Ruby 1.9 / 2.0 though.

p str.gsub(/#{Regexp.quote(left)}(.\*?)(?=#{Regexp.quote(right)})/m,  
“#{left}hello”)

David V.
