string functions? like search replace regex? - Kdb+ / qStudio
Home › Forums › Kdb+ / qStudio › string functions? like search replace regex?
- This topic has 3 replies, 3 voices, and was last updated 7 years, 11 months ago by admin.
-
AuthorPosts
-
April 2, 2013 at 4:17 pm #187
BobGuestWhat string handling functions does kdb provide?
Does it handle regular expressions? Searching for strings like?July 26, 2013 at 10:52 am #520
John DempsterKeymasterWhen selecting based on the condition of a string or char array column, “like” is normally used.
The verb like takes two arguments
1. The list of strings to search
2. The pattern to be searched for
The pattern has certain meta characters like *,? and [] that allow pattern matching.
However full regular expressions are not supported.Here are some examples:
q)t:([] a:til 6; name:(“John Smith”;”John Doe”;”Kate Beck”;”George Bush”;”Isaac Asimov”; “Dave Beck”))
q)t
a name
—————-
0 “John Smith”
1 “John Doe”
2 “Kate Beck”
3 “George Bush”
4 “Isaac Asimov”
5 “Dave Beck”/ perfect matches, rely on correct case
q)select from t where name like “John Smith”
a name
————–
0 “John Smith”
q)select from t where name like “John smith”
a name
——/ * – matches any number of characters
q)select from t where name like “John*”
a name
————–
0 “John Smith”
1 “John Doe”/ ? – Represents any single character
q)select from t where name like “???? Beck”
a name
————-
2 “Kate Beck”
5 “Dave Beck”/ [] – square brackets contain allowed character sets
q)select from t where name like “John [S]*”
a name
————–
0 “John Smith”q)select from t where name like “John [SD]*”
a name
————–
0 “John Smith”
1 “John Doe”Here we used it to match a table with a string column, you can use it on a plain list of strings or symbols as well.
Regards,
JohnFebruary 4, 2014 at 12:25 am #40282
John DempsterKeymasterSomeone has actually written up a guide to importing a regular expression library:
http://code.kx.com/wiki/Cookbook/regexkdb+ 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;
}
}December 4, 2016 at 2:25 pm #111315
adminKeymasterWe’ve added a full listing of string functions here:
http://www.timestored.com/kdb-guides/kdb-string-functions -
AuthorPosts
The topic ‘string functions? like search replace regex?’ is closed to new replies.