Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
767 views
in Technique[技术] by (71.8m points)

mysql - Find and replace string with placeholders in SQL

Is it possible to search for a string like this:

http://getfile1.posterous.com/getfile/files.posterous.com/temp-2013-03-30/kBpHaviyoumcvCEHnGnrDfwHwmJIrnqrHrxhCnHwfBIHuDsxbemHlxwEwCdi/IMG_6870.jpg

while some parts are placeholders like:

http://getfile%.posterous.com/getfile/files.posterous.com/temp-2013-03-%/%/IMG_6870.jpg

and replace it with:

http://my.domain.com/wordpress/wp-content/uploads/2013/03/IMG_6870.jpg

Thank you for inputs!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

MySQL support for regular expressions is very limited, but you can try with a combination of SUBSTRING_INDEX and REPLACE string functions, like this:

UPDATE tablename
SET
  url =
  CONCAT(
    'http://my.domain.com/wordpress/wp-content/uploads/',
    REPLACE(
      REPLACE(
        SUBSTRING_INDEX(
          SUBSTRING_INDEX(url, '/', -3),
          '-',
          3),
        'temp-',
        ''),
      '-',
      '/'),
    '/',
    SUBSTRING_INDEX(url, '/', -1));

But you need to make sure that all strings have the same format. Please see fiddle here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...