# Regex replace on a file

**URL:** <https://www.ruby-forum.com/t/regex-replace-on-a-file/117999>\
**Category:** Ruby\
**Created:** [October 23, 2007, 10:34am UTC](https://www.ruby-forum.com/t/regex-replace-on-a-file/117999 "2007-10-23T10:34:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [October 23, 2007, 10:34am UTC](https://www.ruby-forum.com/t/regex-replace-on-a-file/117999/1 "2007-10-23T10:34:06Z")

</div>

I want to do a simple regex replace on a file. I want to replace this  
multiline match

[\\S\\s]\*?

with this string:

How can I do this with ruby assuming my file is: /html/page.html

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [October 23, 2007, 10:34am UTC](https://www.ruby-forum.com/t/regex-replace-on-a-file/117999/2 "2007-10-23T10:34:06Z")

</div>

2007/10/23, eggie5 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I want to do a simple regex replace on a file. I want to replace this  
> multiline match
> 
> [\\S\\s]\*?
> 
> with this string:
> 
> How can I do this with ruby assuming my file is: /html/page.html

Please show us what you have already.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [October 23, 2007, 1:35pm UTC](https://www.ruby-forum.com/t/regex-replace-on-a-file/117999/3 "2007-10-23T13:35:24Z")

</div>

Hi,

Am Dienstag, 23. Okt 2007, 16:50:04 +0900 schrieb eggie5:

> I want to do a simple regex replace on a file. I want to replace this  
> multiline match

As far as I see for multilines the -i option doesn’t work.  
You could do something like

whole = File.read “fname”  
whole.gsub! /…/m, “…”  
File.open “fname.mod” do |f| f.write whole end

Sorry, but I suppose the best tool for this task is Vim.

Bertram
