4

Does the Find and Replace tool support regex? I need to remove a certain section of an image url for all images in the 'field_body' column.

So if I had an image referenced as such

http://craft.dev/uploads/image-600x400.png

I would be able to find all images that match that pattern and return the url to be

http://craft.dev/uploads/image.png

Now I have very little experience with Regex but would this be a safe pattern to use (if I can with Craft that is...)?

(-[0-9]+x[0-9]+)
6
  • We already had a very similar question: "How can I perform a RegEx match in Twig?"
    – carlcs
    Commented Jun 27, 2014 at 16:49
  • Thanks. It is similar I guess, but not entirely the same. I want to permanently replace the content rather than to rely on using twig for something like this. The URL's shouldnt need to change after this replacement process. Commented Jun 27, 2014 at 16:53
  • 1
    Sorry, did't read this properly. So your actual files are already renamed and now you want to change that in the db, right? What about using Sequel or sth. to change that in the db directly?
    – carlcs
    Commented Jun 27, 2014 at 18:46
  • Sort of. The images were generated by Wordpress from the source image and I want to reference that source image instead of the smaller image, otherwise I'll have 40k photos that I will never use! I could do it directly in the db I guess! Commented Jun 27, 2014 at 19:02
  • 1
    Yes I meant Sequel Pro or sth. (=something :)) like that. Sorry for confusion, also a quick google search made me aware of the fact, that there is no built-in regex functionality in MYSQL or Sequel Pro. There are plugins available, but maybe this is all a little overkill for your single task. I'd rather export that single table and use an external app for the regex (I have no experience with that). I suppose you already entered content to some asset fields and/or related your assets to entries, so that a reimport of your full-size assets is no option?!
    – carlcs
    Commented Jun 28, 2014 at 10:06

2 Answers 2

6

Nope, because MySQL doesn't support RegEx replacements in SQL statements. It's been an open feature request since 2007, so feel free to add your vote.

There are some potential workarounds listed here, too.

3
  • Yeah the more I researched, the less hope I had that there was a solution for it. Guess the easiest would be to dump out the db & do in directly in my IDE? Commented Jun 30, 2014 at 23:47
  • Yeah, that's what I'd do.
    – Brad Bell
    Commented Jul 1, 2014 at 7:15
  • As of MariaDB 10+ regex is supported, would be really nice to include this in CraftCMS 3 as an option for MariaDB users: stackoverflow.com/a/26179601/776167 Commented Dec 19, 2017 at 15:06
1

I'll add my two cents here, since it appears you are removing Wordpress created images (which I also did).

I wrote a regex initially to find all these duplicated files (this command works specifically on OSX, but is pretty portable):

find -E . -regex '.*\-[0-9]{1,4}\x[0-9]{1,4}\.(png|gif|jpg)$'

I then wrote a minor change to delete them:

find -E . -regex '.*\-[0-9]{1,4}\x[0-9]{1,4}\.(png|gif|jpg)$' -delete

I figure you can use this same regex pattern to find/replace in a database dump locally, which this answer here points to: https://stackoverflow.com/a/9456574/776167

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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