extract number from string in kdb

2019-08-09 08:27发布

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

标签: kdb q-lang
3条回答
The star\"
2楼-- · 2019-08-09 08:38

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
查看更多
手持菜刀,她持情操
3楼-- · 2019-08-09 08:47

I can suggest the following if we assume only one number in the string:

q)"AZXER_1234_MARKET"inter .Q.n
"1234"
q)"A_5643_B"inter .Q.n
"5643"

Then of course you can cast it to any type you want.

查看更多
乱世女痞
4楼-- · 2019-08-09 08:53

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
查看更多
登录 后发表回答