在 sqlite3 中用 REGEXP 替换字符串的一部分 [英] replace a part of a string with REGEXP in sqlite3

查看:120
本文介绍了在 sqlite3 中用 REGEXP 替换字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了 REGEX 支持

I installed REGEX support with

apt-get install sqlite3 sqlite3-pcre

现在我可以在 bash 控制台上的查询中使用 REGEX,例如

now I can use REGEX in my queries on the bash console like

DB="somedb.db"
REGEX_EXTENSION="SELECT load_extension('/usr/lib/sqlite3/pcre.so');"
sqlite3 $DB "$REGEX_EXTENSION select * from sometable where name REGEXP '^[a-z]+$'"

但是如何使用正则表达式通过 sqlite 查询更新字符串?

But how can I update a string with an sqlite query using regex?

推荐答案

Sqlite 默认不提供 regex_replace 函数.您需要将其作为扩展加载.这是我设法做到的.

Sqlite by default does not provide regex_replace function. You need to load it as an extension. Here is how i managed to do it.

下载扩展 (icu_replace) 的 C 代码

使用

gcc --shared -fPIC -I sqlite-autoconf-3071100 icu_replace.c -o icu_replace.so

并且在上面提到的命令后运行的sqlite3中运行并创建了一个文件icu_replace.so

And in sqlite3 runn following command post above mentioned command has run and create a file icu_replace.so

SELECT load_extension(' path to icu_replace.so', 'sqlite3_extension_init') from dual;

在此之后,您将能够使用这样的功能:-

After this you will be able to use such a function as :-

select regex_replace('\bThe\b',x,'M') from dual;

这篇关于在 sqlite3 中用 REGEXP 替换字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆