Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract number from string in kdb

Tags:

q-lang

kdb+

I am quite new to kdb+q. I've come across this problem of extracting a number out of string.

Any suggestions?

Example:

"AZXER_1234_MARKET" should output 1234 //Assume that there is only one number in the 

string

like image 492
pikachuchameleon Avatar asked Dec 20 '25 01:12

pikachuchameleon


2 Answers

Extract the numbers then cast to required type.

q){"I"$x inter .Q.n} "AZXER_1234_MARKET"
1234i

q){"I"$x inter .Q.n} "AZXER_123411_MARKET"
123411i
q){"I"$x inter .Q.n} "AZXER_1234_56_MARKET"
123456i
q){"I"$x inter .Q.n} "AR_34_56_MAT"
3456i
like image 54
John at TimeStored Avatar answered Dec 22 '25 21:12

John at TimeStored


If you have multiple numbers, here is a variation of the above, which allows for multiple numbers in one string

q)nums:{"I"$((where n&differ  n:x in .Q.n) cut x) inter\: .Q.n}
q)nums "this is 123 and this is 56"
123 56i
like image 26
JPC Avatar answered Dec 22 '25 19:12

JPC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!