replace a part of a string with REGEXP in sqlite3

Solution 1:

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;

Solution 2:

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 -