# Best way to search and replace these regexes?

**URL:** https://meta.discourse.org/t/best-way-to-search-and-replace-these-regexes/102146
**Category:** Support
**Created:** [16.Ноябрь.2018 03:36:38 UTC](https://meta.discourse.org/t/best-way-to-search-and-replace-these-regexes/102146 "2018-11-16T03:36:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![j127](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j127/32/79093_2.png) [@j127](https://meta.discourse.org/u/j127)
#### Post date: [16.Ноябрь.2018 03:36:38 UTC](https://meta.discourse.org/t/best-way-to-search-and-replace-these-regexes/102146/1 "2018-11-16T03:36:38Z")

</div>

After searching around the forum here, I think I might be looking for `rake posts:remap`, but it wasn’t clear whether it works with regex. This is what I’m trying to do:

During an import, a couple of things in the raw post content needs updating:

The `[quote]` tags aren’t always in the format that Discourse expects, so I want to change things like this:

```plaintext
[quote=username]lorem

ipsum[/quote]

```

to this:

```plaintext
[quote=username]
lorem

ipsum
[/quote]

```

by inserting `\n` characters, probably something like `\[quote(.*?)\]` to `\[quote$1\]\n` and `\[/quote\]` to `\n\[/quote\]` whenever needed. (I still need to look up Ruby regex syntax.)

The images also broke because we moved to a subdomain, so tags like this:

```plaintext
<img src="/files/some/path/image.jpg" alt="some alt text">

```

have to be updated to

```plaintext
<img src="https://example.com/files/some/path/image.jpg" alt="some alt text">

```

Is `rake posts:remap` what I’m looking for, and is this general syntax correct?  
`rake posts:remap["\[quote(.*?)\]","\[quote$1\]\n"]`  
(I know my regex is wrong, but I’m going to figure that out later tonight.)

Would I run something like that once for each change, or is there a way to do both in one pass?

---

<div class="post-metadata">

### Author: ![fhe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fhe/32/113345_2.png) [@fhe](https://meta.discourse.org/u/fhe)
#### Post date: [16.Ноябрь.2018 07:21:12 UTC](https://meta.discourse.org/t/best-way-to-search-and-replace-these-regexes/102146/2 "2018-11-16T07:21:12Z")

</div>

> [@j127](#):
>
> Is `rake posts:remap` what I’m looking for, and is this general syntax correct?  
> `rake posts:remap["\[quote(.*?)\]","\[quote$1\]\n"]`

You can use `rake posts:remap` to do this, but I think you need to specify `"regex"` as 3rd parameter, otherwise the other parameters are treated as strings.

Another option is to use the rails console and use Ruby to do the update:

```plaintext
imported_posts = Post.where("id < your_last_imported_post_id")
imported_posts.find_each do |post|
  raw = post.raw.gsub(/<your magic regex here>/, "<your replacement here>")
  post.update_column(:raw, raw)
end

```

This way, you can also perform a dry run first, output the possible matches and accompanying replacements and see whether everything works as intended. Multiple replacements in one run are of course also possible then.

---

<div class="post-metadata">

### Author: ![j127](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j127/32/79093_2.png) [@j127](https://meta.discourse.org/u/j127)
#### Post date: [16.Ноябрь.2018 07:41:37 UTC](https://meta.discourse.org/t/best-way-to-search-and-replace-these-regexes/102146/3 "2018-11-16T07:41:37Z")

</div>

> [@fhe](#):
>
> Another option is to use the rails console and use Ruby to do the update:

Thanks, that’s perfect.

---

<div class="post-metadata">

### Author: ![j127](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j127/32/79093_2.png) [@j127](https://meta.discourse.org/u/j127)
#### Post date: [16.Ноябрь.2018 20:11:49 UTC](https://meta.discourse.org/t/best-way-to-search-and-replace-these-regexes/102146/4 "2018-11-16T20:11:49Z")

</div>

Does anyone know if it will send email notifications when I resave the posts like that with `.update_column(:raw, raw)`? The forum is going to send a large number of digests today and tomorrow, and I don’t want to turn emails off while it’s trying to do that.

Also, do I need to rebake all of the posts afterwards?

---

<div class="post-metadata">

### Author: ![j127](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j127/32/79093_2.png) [@j127](https://meta.discourse.org/u/j127)
#### Post date: [17.Ноябрь.2018 01:33:53 UTC](https://meta.discourse.org/t/best-way-to-search-and-replace-these-regexes/102146/5 "2018-11-17T01:33:53Z")

</div>

For anyone else looking for an answer: as far as I can tell from testing a post, using `update_column` doesn’t sent email notifications or create a new revision. Rebaking _is_ needed for the changes to show up.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [17.Декабрь.2018 01:34:07 UTC](https://meta.discourse.org/t/best-way-to-search-and-replace-these-regexes/102146/6 "2018-12-17T01:34:07Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
