18

I installed REGEX support with

apt-get install sqlite3 sqlite3-pcre

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]+$'"

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

rubo77
  • 19,527
  • 31
  • 134
  • 226

3 Answers3

13

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.

Download this C code for the extension (icu_replace)

Compile it using

gcc --shared -fPIC -I sqlite-autoconf-3071100 icu_replace.c -o 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;
Vishal Tyagi
  • 560
  • 9
  • 26
3

The following builds latest sqlite with dynamic library support, and compiles ICU extension and regex_replace extension. It also assumes debian-based linux distributive:

sudo apt build-dep sqlite3 # fetches dependencies to compile sqlite3

mkdir sqlite-compilation
cd    sqlite-compilation

wget -O sqlite.tar.gz https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release

tar xzf sqlite.tar.gz

mkdir build
cd    build
  ../sqlite/configure
  make OPTS='-DSQLITE_ENABLE_LOAD_EXTENSION'
  ./sqlite3 -cmd 'pragma compile_options;' <<< .exit
cd -


# https://sqlite.org/src/dir?name=ext/icu
cd sqlite/ext/icu
  sed -i 's/int sqlite3_icu_init(/int sqlite3_extension_init(/' icu.c
  sed -i 's/int sqlite3IcuInit(/int sqlite3_extension_init(/' sqliteicu.h
  gcc -g -O2 -shared icu.c -fPIC -I ../../../build `pkg-config --libs icu-i18n` -o libSqlite3Icu.so
  cp libSqlite3Icu.so ../../../build/
cd -

# https://github.com/gwenn/sqlite-regex-replace-ext
cd sqlite/ext
  wget -O sqlite-regex-replace-ext-master.zip https://github.com/gwenn/sqlite-regex-replace-ext/archive/master.zip
  unzip   sqlite-regex-replace-ext-master.zip
  cd      sqlite-regex-replace-ext-master
    gcc -g -O2 -shared icu_replace.c -fPIC -I ../../../build -o libSqlite3IcuReplace.so
    cp libSqlite3IcuReplace.so ../../../build/
  cd -
cd ../../

In result you will have:

build/sqlite3              # sqlite3 binary
build/libSqlite3Icu.so     # unicode support
build/libSqlite3IcuReplace # regex_replace function

Test:

cd build
  sqlite3 <<< "
.load ./libSqlite3Icu
.load ./libSqlite3IcuReplace
select regex_replace('^a', 'aab', 'b');
.exit
  " # should output: bab
cd -
Adobe
  • 12,967
  • 10
  • 85
  • 126
2

For me the above answers didn't work because of some missing parameters in the gcc command.

This works for me:

git clone https://github.com/gwenn/sqlite-regex-replace-ext.git
cd sqlite-regex-replace-ext-master/
./icu_replace.sh

Now you should be able to load the extension using: SELECT LOAD_EXTENSION('path-to-icu_replace.so');

Laurent Dhont
  • 1,012
  • 1
  • 9
  • 22