Depends a bit on your database server, what it supports, but here's another take using nothing but the database:
> SELECT REGEXP_REPLACE('my text before [shareprints some_othertype gallery_id="14629" gallerytype = 0, etc] my text [shareprints some_othertype gallery_id="1" gallerytype = 0, etc] my text after',
'\\[shareprints [^\\]]*gallery_id="([0-9]+)"[^\\]]*\\]',
'\[foogallery gallery_id="\\1"]');
+--------------------------------------------------------------------------------------------------+
| REGEXP_REPLACE(...) |
+--------------------------------------------------------------------------------------------------+
| my text before [foogallery gallery_id="14629"] my text [foogallery gallery_id="1"] my text after |
+--------------------------------------------------------------------------------------------------+
Some care is required to not make the regexp too greedy, which is why [^\\]]*
is used (instead of .*
) to catch anything not needed within the tag(and not outside it). Backreferences(parentheses) are used for the things that should be transferred, and then a corresponding \N (N for number) in the replacement pattern.
Then when you're happy with the regexp you can use
UPDATE mytable
SET mycol=REGEXP_REPLACE(mycol,'myregexp','myreplacement')
WHERE <my condition in case I do not want to update all rows>
..or similar to keep the database busy for some time ;)