Reply To: string functions? like search replace regex? - Reply To: string functions? like search replace regex?

Home Forums Kdb+ / qStudio string functions? like search replace regex? Reply To: string functions? like search replace regex?

#40282

John Dempster
Keymaster

Someone has actually written up a guide to importing a regular expression library:
http://code.kx.com/wiki/Cookbook/regex

kdb+ has some builtin regex features, for use with like and ssr.

For those who need something more flexible, it’s possible to leverage regex libs such as re2, described below.

The home for re2 can be found at [1] The code below was compiled for kdb+v3.1 with this release [2] The k.h file can be downloaded from [3] For 64bit linux, this can be compiled as

g++ -m64 -O2 re2.cc -o re2.so -I . re2/obj/libre2.a -DKXVER=3 -shared -static

and the resulting re2.so should be copied into $QHOME/l64 subdirectory.

It can then be loaded and called in kdb+ via

q)f:`re2 2:(`FullMatch;2) / bind FullMatch to f
q)f[“hello world”;”hello ..rld”]

#include
#include
#include //malloc
#include
#include”k.h”

using namespace re2;

extern “C” {
Z S makeErrStr(S s1,S s2){Z __thread char b[256];snprintf(b,256,”%s – %s”,s1,s2);R b;}
Z __inline S c2s(S s,J n){S r=(S)malloc(n+1);R r?memcpy(r,s,n),r[n]=0,r:(S)krr((S)”wsfull (re2)”);}
K FullMatch(K x,K y){
S s,sy;K r;
P(x->t&&x->t!=KC&&x->t!=KS&&x->t!=-KS||y->t!=KC,krr((S)”type”))
U(sy=c2s((S)kC(y),y->n))
RE2 pattern(sy,RE2::Quiet);
free(sy);
P(!pattern.ok(),krr(makeErrStr((S)”bad regex”,(S)pattern.error().c_str())))
if(!x->t||x->t==KS){
J i=0;
K r=ktn(KB,x->n);
for(;in;i++){
K z=0;
P(!x->t&&(z=kK(x)[i])->t!=KC,(r0(r),krr((S)”type”)))
s=z?c2s((S)kC(z),z->n):kS(x)[i];P(!s,(r0(r),(K)0))
kG(r)[i]=RE2::FullMatch(s,pattern);
if(z)free(s);
}
R r;
}
s=x->t==-KS?x->s:c2s((S)kC(x),x->n);
r=kb(RE2::FullMatch(s,pattern));
if(s!=x->s)free(s);
R r;
}
}