Replace random text between two known strings

Asked

Viewed 37 times

1

I need to replace a random text between two fixed strings in a database created by Wordpress.

Database: "database"
Table: "wordpress_posts"
Field: "post_content"
ID: "6450"

For example, I have the text <inicio do texto fixo> texto aleatório </fim do texto fixo>, where we have "random text" I want it to be "new text", as follows:

<inicio do texto fixo> novo texto </fim do texto fixo>

I had asked a question, but it was closed because information was missing, based on a comment they left I reached the code below:

UPDATE wordpress_posts
SET post_content = REGEXP_REPLACE(
    post_content, 'texto aleatório', 'novo texto')
WHERE `wordpress_posts`.`ID`='6450';

But I don’t know where to inform strings that are fixed to precisely change the text that is between them.

  • 1

    In accordance with the documentation, the second function parameter REGEXP_REPLACE accepts the pattern to be replaced. That is, you can use it for your regular expression.

  • @Andersoncarloswoss following the examples I left the command as follows 'UPDATE wordpress_posts SET post_content = REGEXP_REPLACE( post_content, '<fixed text start>(.| n)*? </fixed text end>', 'new text') WHERE wordpress_posts.ID='6450'' but no effect, nothing is changed, see http://prntscr.com/ogxm7y

  • changed the expression to (.| n)*? , to ensure that nothing was wrong with the typed words, but it also came to nothing http://prntscr.com/ogxmwt

1 answer

0


In the given example, the solution would be this (remembering that this only works on Mysql 8.0+):

UPDATE wp_posts
SET post_content = REGEXP_REPLACE(
    post_content, '^<inicio do texto fixo>.+<\/fim do texto fixo>$', '<inicio do texto fixo> novo texto </fim do texto fixo>')
WHERE `wp_posts`.`ID`='6450';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.