Replace a string in all posts

(Kane York) #43

We’re talking about RegEx:

How to replace a string/character in topic title?
(Felix Freiberger) #44

Wait, isn’t that just about rebaking, and he also needs to run a rake posts:remap first? If so, does remapping also support regular expressions?

(Gerhard Schlager) #45

Oops :blush: You are right. I shouldn’t post that late at night :wink:

The posts:remap rake task doesn’t support regular expressions, but it is using the SQL LIKE operator…
Unfortunately the find parameter is reused within a regex. But with a few changes this could certainly work.
PR welcome. :slight_smile:


Just submitted a PR:

> rake posts:remap['pattern', 'replace', type]
> rake posts:delete_word['pattern', type]

After having implemented the change, I came to realize that supporting regex pattern for remap and delete_word might be a bit dangerous? There are confirmation prompts in place, but these actions are still nonetheless irreversible and enabling regex matching might include a much larger set of posts than intended.

(Joffrey Jaffeux) #47

Maybe we could have something like rake post:remap_plan['pattern', 'replace', type] which would not change anything, but only display, something like the 100 first impacted posts (and what is going to change) ? So you could have a better idea of what is going to happen. Thoughts?

(Kane York) #48

The convention is to accept a -n / --dry-run option for that, so it’s easy to delete when you’re ready.

(Joffrey Jaffeux) #49

The question is not on the syntax :wink: and it’s a rake task so having options is doable but a little bit unexpected so rake post:remap['pattern', 'replace', type] DRY_RUN=1 would probably be more in the spirit of rake

(Joseph Coates) #50

So, if one was to use this to change a username…

@jcoates <a href="/u/jcoates" class="mention ">@jcoates</a>

Can you just find/replace the whole html mention link? Is there anywhere in the database that mentions are stored other than in the posts themselves?


Yeah I was thinking along those lines.

Displaying the information in a clear and concise manner is critical though, especially when it comes to a large number of posts and/or lengthy matches. I’ll look into some possibilities.

AFICT, that’ll work and there are nothing else you’ll need to change.

(Joseph Coates) #52

hmmm… just thinking… what about the user’s previous posts. Is there a need to find/replace the author info or is this just built off of the author’s user ID?

(Tobias Eigen) #53

Incidentally, i ran into an odd problem where people got notified when i ran this command to rename a group that was mentioned in a bunch of posts going back a few years. It was not consistent but somehow some group members got email notifications. I did not take the time to troubledhoot and just apologized to those affected. But next time i will try to remember to suppress email notifications before doing this again.

(Jeff Atwood) #54

There is a bug around editing we have had for some time where it will trigger re-notification inappropriately in some cases.

(Arpit Jalan) split this topic #55

6 posts were split to a new topic: How to replace a string/character in topic title?


how can i add a new line/line break before or after a specific string which i want to replace? same for whitespace/space bar.

i want to remove [img]-tags, some of them nested inside a [url]-tag, which prevents the remote image download.


Adding <br/> before/after the replacement string should work:

rake posts:remap["find","replace<br/>"]

This initially sounded like a #bug, but I just tested it on try and it works fine:



adding a new line worked. thanks @xrav3nz.

the nested [img] seem to be another problem with some specific urls, but thanks!

(Anton) #59

Any chance to get replacement patterns as key/value pairs in admin settings, with preview and automatic “watch” mode for each new post or post edit?

Use-case: I’d like to replace ))))) to :) automatically. Some people put lots of parenthesis instead of emojis and it looks terrible, the text looks spoiled by parenthesis. I’d like not to bother people by asking them not to do so, but rather replace automatically with a regex. There are more examples in punctuation normalization:

........... => ...
'!!!` => ‘!’


This could be used to strip out signatures that come by way of posts made by email.

(Anton) #61

Yep, we have this issue too


It is possible to remove all Mybb text style tags from the posts.

I am talking about [] tags like or [color="#fffff"] [/color]. I want to search all [] and [/] tags, whatever the value is, and remove them. And i need any suggestion about that.

I wan to delete “[font=‘Open Sans’, sans-serif]” string with rake posts, but it doesnt work.

rake posts:delete_word["[font=‘Open Sans’\,\ sans-serif]"]



0 posts updated!