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]+)
  • We already had a very similar question: "How can I perform a RegEx match in Twig?" – carlcs Jun 27 '14 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. – Sean O'Grady Jun 27 '14 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 Jun 27 '14 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! – Sean O'Grady Jun 27 '14 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 Jun 28 '14 at 10:06
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.

  • 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? – Sean O'Grady Jun 30 '14 at 23:47
  • Yeah, that's what I'd do. – Brad Bell Jul 1 '14 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 – hisnameisjimmy Dec 19 '17 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, privacy policy and cookie policy

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