Answer the question
In order to leave comments, you need to log in
How to replace SQL with regular expressions?
Help me please!
I have a SQL posts table and a column contant ,
you need to find in this column all links of the form http://sitename.ru/files/2019/05/examplename.pdf
and replace them with
[chortcode url="http://sitename.ru/files/2019/05/examplename.pdf"]
http://sitename.ru/files/
and .pdf
should remain as it is. UPDATE posts SET contant= REPLACE(contant, 'http://sitename.ru/files/', '[chortcode url="http://sitename.ru/files/');
UPDATE posts SET contant= REPLACE(contant, '.pdf', '.pdf"]');
http://sitename.ru/files/
, but they start, for example , you [asdf url="http://sitename.ru/files/
don’t need to touch them in general, http://sitename.ru/files/
there should be nothing before, but again, I don’t know either how to do this, and did not understand how to work with regular expressions, maybe it will be easier to figure it out using a ready-made example. http://sitename.ru/files/2019/05/examplename.pdf
[drugoyshortcode url="http://sitename.ru/files/2019/05/examplename.docx"] - (его не трогаем потому что до ссылки другой шорткод и не нужное расширение файла)
http://sitename.ru/files/2018/04/myname.pdf
http://sitename.ru/files/2019/05/examplename.xlsx - (его тоже не трогаем, потому что не нужное расширение файла)
http://sitename.ru/files/2019/05/yourname.pdf
[sortcode url="http://sitename.ru/files/2019/05/examplename.pdf"]
[drugoyshortcode url="http://sitename.ru/files/2019/05/examplename.docx"]
[sortcode url="http://sitename.ru/files/2018/04/myname.pdf"]
http://sitename.ru/files/2019/05/examplename.xlsx
[sortcode url="http://sitename.ru/files/2019/05/yourname.pdf"]
http://sitename.ru/files/2019/05/vashenename.pdf
Answer the question
In order to leave comments, you need to log in
I found the following solution, thanks zhaar
UPDATE posts
SET content = REPLACE(content, 'http://sitename.ru/files/', '[chortcode url="http://sitename.ru/files/'),
content = REPLACE(content, '.pdf', '.pdf"]')
WHERE content like 'http://sitename.ru/files/%';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question