D
D
Darth Moul2019-05-21 14:08:33
SQL
Darth Moul, 2019-05-21 14:08:33

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"]

That is, everything that is between http://sitename.ru/files/and .pdfshould remain as it is.
I’ll make a reservation right away that to create two queries of the form
UPDATE posts SET contant= REPLACE(contant, 'http://sitename.ru/files/', '[chortcode url="http://sitename.ru/files/');
UPDATE posts SET contant= REPLACE(contant, '.pdf', '.pdf"]');

not an option, since there may be lines that also contain http://sitename.ru/files/, but they start, for example , you [asdf url="http://sitename.ru/files/don’t need to touch them in general,
you can probably decide without regular expressions, for example, somehow indicate that 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.
I apologize in advance for the curly question
------------------------------ ADDITIONED -------------------- --------------
Simply put, there are the following rows with the value of the content columns :
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

it should turn out like this
[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

1 answer(s)
D
Darth Moul, 2019-05-21
@ginesufa

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 question

Ask a Question

731 491 924 answers to any question