Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to apply a text replace on my WordPress MySQL database. I need to delete part of the current images html code, but this part of the text is variable, althouth there is a part that never changes.

Is there some wildcard that I can use on a MySQL replace?

Example: I would like to replace this:

<a target="_self" href="actualidad/tecnologia/4455-windows-7-surpasses.html"><img src=”" width="215" height="156" /></a>


<img src=”" width="215" height="156" />

And delete the href part, so I was thinking in a replace similar to this one:

update wp_posts set post_content = REPLACE 
(post_content, '<a target="_self" href=*<img','<img');

Is there a "*" (or regexp) that takes whatever there is between the 'href' and the '

share|improve this question
up vote 3 down vote accepted

I usually cheat and dump the database to text, and then use sed (or Notepad++ if the database is small enough). Barring that, it's easier to do it programmatically using Perl or Python.

Doing this kind of manipulation with straight SQL is painful. If you DO do it, I'd recommend strongly that you avoid REPLACE. This isn't what it's normally used for. If you're going to do a bulk change on existing rows, use UPDATE by itself instead.

share|improve this answer
I guess a SUBSTR or MID approach has more clarity than an outright UPDATE. UPDATE is simpler, but as you just said, avoiding REPLACE is better. +1 for you. – RolandoMySQLDBA May 17 '11 at 15:36
Thanks for the suggestion... I'm affraid I'm just a novice in regexp, and I don't know how to program/use Perl or Python to do this. Getting a SQL backup and opening it on notepad++ was one of my ideas, but I didn't know how to replace this kind of 'wildcard sentences' either :( – javipas May 17 '11 at 15:40
@javipas: MySQL is pretty friendly about letting you export to a massive SQL statement. Most of the free tools support this. You can download the trial version of Navicat, and use export it most any file format. – Satanicpuppy May 17 '11 at 15:58
There's no problem with the SQL dump. I can do it both through phpmyadmin or through the mysql client on my server. The problem I have is what to do with it after that :( I don't know how to solve the search and replace problem with Perl/Python. Any other alternative? Maybe a specific solution with some other text editor supporting regexp? – javipas May 17 '11 at 16:28
@javipas: Notepad++ supports regex, huge files, macros, you name it. You could also dump into excel (or anything) using ">" as a column delimiter. – Satanicpuppy May 17 '11 at 16:40

% does not work in this scenario?

share|improve this answer

Unfortunately, there is no further regular expression API available.

People have taken cracks at it in the MySQL 4.0 days, but such efforts did not make it into the mainstream MySQL binaries.

Your UPDATE statement should be enough.

update wp_posts set post_content = REPLACE (post_content, '<a target="_self" href=*<img','<img');

Even if such API existed, there is no sense in applying it any better than you already have. Your UPDATE does a full table scan. Applying a WHERE using REGEXP would still results in a full table scan. You original UPDATE is best for the given scenario.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.