MySQL搜索并替换为随机字符

时间:2019-04-29 15:27:55

标签: mysql wordpress search

我在文本中有一些网址UPDATE wp_posts SET post_content = REPLACE(post_content, 'itok=[[:xdigit:]]{8}', '') WHERE post_content LIKE 'itok=[[:xdigit:]]{8} '; 我需要在每个URL中删除此垃圾令牌LIKE this [a-fA-F0-9]{8},所有令牌显然都是随机的:)。 我尝试直接在数据库中执行以下操作:

import os,sys

folder = "C:/Users/workexp/Downloads"
for filename in os.listdir(folder): 
    if os.path.isfile(filename): 
        file_name, file_extension = os.path.splitext(filename)
        if file_extension == '.nc':
            os.rename(filename,file_name+'.csv')

但是我找不到像这样的任何标记。 @Override @Transactional(readOnly = true) public Collection<Account> searchAccounts(String partOfName) { Collection<Account> accounts = accountDao.getAll(); CollectionUtils.filter(accounts, account -> account.getName().toLowerCase().contains(partOfName.toLowerCase()) || account.getSurname().toLowerCase().equalsIgnoreCase(partOfName.toLowerCase())); return accounts; } 也无济于事。有什么建议吗?感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

只有拥有MySQL 8.0的用户才能使用正则表达式。

但是,如果您的链接位于单独的字段中,则可以创建并使用SP清理它们:

DELIMITER $$
CREATE FUNCTION CLEANUP(
      aString VARCHAR(255)
    , aName VARCHAR(15)
)
RETURNS VARCHAR(255)
BEGIN
    SET @u = SUBSTRING_INDEX(aString, '?', 1);
    SET @q = SUBSTRING_INDEX(aString, '?', -1);

    IF @q = aString THEN
        RETURN aString; -- no '?' char found
    ELSE
        SET @f = LOCATE(CONCAT(aName, '='), @q);
        SET @query = IF(@f > 1, SUBSTR(@q, 1, @f - 1), '');
        IF @f > 0 THEN
            SET @t = LOCATE('&', @q, @f + LENGTH(aName) + 1);
            IF @t > 0 THEN
                SET @query = CONCAT(@query, SUBSTR(@q, @t + 1));
            END IF;
        END IF;

        IF @query = '' THEN
            RETURN @u;
        ELSE
            RETURN CONCAT(@u, '?', TRIM('&' FROM @query));
        END IF;
    END IF;
END
$$
DELIMITER ;

然后:

SELECT
      CLEANUP('https://example.com/public/images/someimage.jpg?itok=WDGFySy&b=2', 'itok')
    , CLEANUP('https://example.com/public/images/someimage.jpg?itok=WDGFySy', 'itok')
    , CLEANUP('https://example.com/public/images/someimage.jpg?a=1&itok=WDGFySy', 'itok')
    , CLEANUP('https://example.com/public/images/someimage.jpg?a=1&itok=WDGFySy&b=2', 'itok')
;