# kdb like query

**URL:** <https://forum.kx.com/t/kdb-like-query/8664>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [March 14, 2014, 6:27pm UTC](https://forum.kx.com/t/kdb-like-query/8664 "2014-03-14T18:27:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikas\_agarwal1](https://avatars.discourse-cdn.com/v4/letter/v/22d042/32.png) [@vikas\_agarwal1](https://forum.kx.com/u/vikas_agarwal1)\
**Post date:** [March 14, 2014, 6:27pm UTC](https://forum.kx.com/t/kdb-like-query/8664/1 "2014-03-14T18:27:00Z")

</div>

Hi,

I am not able to figureout why following like function gives error:

“dir1/dir2/dir3/dir5” like “dir1/\*/\*/dir5”

Any suggestions will be helpful.

---

<div class="post-metadata">

**Author:** ![manni\_patel](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@manni\_patel](https://forum.kx.com/u/manni_patel)\
**Post date:** [March 14, 2014, 6:30pm UTC](https://forum.kx.com/t/kdb-like-query/8664/2 "2014-03-14T18:30:00Z")

</div>

q only supports simple wildcarding?

---

<div class="post-metadata">

**Author:** ![Attila](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Attila](https://forum.kx.com/u/Attila)\
**Post date:** [March 14, 2014, 6:30pm UTC](https://forum.kx.com/t/kdb-like-query/8664/3 "2014-03-14T18:30:00Z")

</div>

it is simply not supported  
kdb+'s regexp support it minimal and to the point

you could do this with

q)all"dir1/dir2/dir3/dir5" like/:(“dir1/\*”;“\*/dir5”)

1b

Cheers,

? Attila

? Attila

---

<div class="post-metadata">

**Author:** ![sohagan8571](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@sohagan8571](https://forum.kx.com/u/sohagan8571)\
**Post date:** [March 26, 2014, 10:17pm UTC](https://forum.kx.com/t/kdb-like-query/8664/4 "2014-03-26T22:17:00Z")

</div>

Better late than never…only getting round to reading google groups now.

If you have consistency in your directory structure use “?” instead of “\*”.

“dir1/dir2/dir3/dir5” like “dir1/???/???/dir5”

1b

May or may not help.

Thanks,

Sean

---

<div class="post-metadata">

**Author:** ![vikas\_agarwal1](https://avatars.discourse-cdn.com/v4/letter/v/22d042/32.png) [@vikas\_agarwal1](https://forum.kx.com/u/vikas_agarwal1)\
**Post date:** [March 27, 2014, 1:39pm UTC](https://forum.kx.com/t/kdb-like-query/8664/5 "2014-03-27T13:39:00Z")

</div>

Hi Sean,&nbsp;

we don’t have fix directory name length that is why i was looking for \*. I end up writing a function that will compare path vs pattern.

I am pasting the code i wrote as well as the test cases. may be if someone can improve the code. I am new to kdb.

CompareKeyWithPath:{[openClosed;pattern;xdb;delim]

&nbsp; &nbsp; &nbsp; &nbsp;$[(count pattern)=1;pattern:(enlist pattern);]&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp;$[(count xdb)=1;xdb:(enlist xdb);]&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp;;patternCnt:count pattern

&nbsp; &nbsp; &nbsp; &nbsp;;xdbCnt:count xdb &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp;;i:0;j:0

&nbsp; &nbsp; &nbsp; &nbsp;;while[i\<patternCnt &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; ;$[“\*”=(pattern i);while[(j\<xdbCnt) and (delim \<\> xdb j);j+:1] ;$[(pattern i)\<\>xdb j;:0;j+:1]] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; ;i+:1

&nbsp; &nbsp; &nbsp; &nbsp;]

&nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp;;$[j\<xdbCnt;$[openClosed=“C”;:0;$[delim\<\>xdb j;:0;]];] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp;;:1

&nbsp; &nbsp; }

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp;/ Closed positive scenarios &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp;;(CompareKeyWithPath[“C”;“dir1”;“dir1”;“/”]=1 &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1/dir2”;“dir1/dir2”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1/dir2/dir3/dir4/dir5”;“dir1/dir2/dir3/dir4/dir5”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“\*/\*/\*/dir5”;“dir1/dir2/dir3/dir5”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1/\*/\*/dir5”;“dir1/dir2/dir3/dir5”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1/\*/dir3/dir5”;“dir1/dir2/dir3/dir5”;“/”]=1 &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1/\*/\*/\*”;“dir1/dir2/dir3/dir5”;“/”]=1 &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp;/ Special case&nbsp;

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“\*”;“dir1/dir2/dir3/dir5”;“/”]=0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“\*”;“dir1”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“\*”;“dir2”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“\*”;“dir2/dir1”;“/”]=0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“\*/\*/\*”;“dir2/dir1”;“/”]=0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“\*/\*/\*”;“dir2/dir1/dir3”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“\*/\*/\*”;“dir2d/ddir1/didr3”;“/”]=1&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; / Closed negative scenarios

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1”;“dir11”;“/”]=0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1”;“dir2”;“/”]=0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1”;“dir1/dir2/dir3/dir4”;“/”]=0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1”;“dir1/dir2”;“/”] =0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1/\*/dir5”;“dir1/dir2/dir3/dir5”;“/”]=0

&nbsp; &nbsp; &nbsp;/ This should never happen “/” should never be in the end of path or key

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“C”;“dir1/\*/\*/\*”;“dir1/dir2/dir3/dir5/”;“/”]=0

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;/ Open Positive scenarios

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1”;“dir1”;“/”]=1 &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1”;“dir1/dir2/dir3/dir4”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1/dir2”;“dir1/dir2/dir3/dir4”;“/”]=1 &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1”;“dir1/dir2”;“/”] =1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1/dir2”;“dir1/dir2”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1/dir2/dir3/dir4/dir5”;“dir1/dir2/dir3/dir4/dir5”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“\*/\*/\*/dir5”;“dir1/dir2/dir3/dir5”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“\*/\*/\*/dir5”;“dirdsd1/dir2/dir3/dir5”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“\*/\*/\*/dir5”;“dir1/dir2/dir3/dir6”;“/”]=0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1/\*/\*/dir5”;“dir1/dir2/dir3/dir5”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1/\*/dir3/dir5”;“dir1/dir2/dir3/dir5”;“/”]=1 &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1/\*/\*/\*”;“dir1/dir2/dir3/dir5”;“/”]=1 &nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1/\*/\*/\*”;“dir1/dir2/dir3/dir5/dir6”;“/”]=1

&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp;/ Open Negative scenarios

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1”;“dir11”;“/”]=0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1”;“dir2”;“/”]=0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1/dir2”;“dir1/dir22/dir3/dir4”;“/”]=0

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“dir1/\*/dir5”;“dir1/dir2/dir3/dir5”;“/”]=0 &nbsp;

;CompareKeyWithPath[“O”;“GENEVA\PFPC”;“GENEVA\PFPC\W201\TRANSACTIONS”;“\”]=1

&nbsp; &nbsp; &nbsp; / Special case&nbsp;

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“\*”;“dir1”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“\*”;“dir2”;“/”]=1

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“\*”;“dir1/dir2/dir3/dir5”;“/”]=1 &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;;CompareKeyWithPath[“O”;“\*”;“dir2/dir1”;“/”]=1

&nbsp; &nbsp; )
