Best way to search and replace these regexes?

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:



to this:



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:

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

have to be updated to

<img src="" 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?

1 Like

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:

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)

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.


Thanks, that’s perfect.

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?

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.


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