cancel
Showing results for 
Search instead for 
Did you mean: 

Replace multiple whitespaces with 1

sututor
New Contributor
Hi,

I am having trouble replace multiple whitespaces with 1. 

E.g., ssr["replace     with    one";"\\s+";" "] does not work. Your help is much appreciated. Thank you.


Tori

5 REPLIES 5

thomas_smyth
New Contributor
Hi,

Regex isn't fully supported in q, hence why using "\\s+" doesn't return the expected result in this case. Here is a basic workaround for your example case:

q){spaces:" "=x; x where not spaces and prev[spaces]}["replace     with    one"]
"replace with one"

If you want to implement regex then I would take a look at this page: http://code.kx.com/wiki/Cookbook/regex.

Regards,
Thomas Smyth
AquaQ Analytics

Hi,

Another option is:

q)cleanText:{x where(or)':[not null x]};

q)cleanText["replace     with    one"]

"replace with one"


Marcus

Nick
New Contributor II
you can user 'over' to keep replacing 2 spaces with 1:

q)ssr[;"  ";" "]/["replace     with    one"]
"replace with one"

or

q)ssr[;"  ";" "] over "replace     with    one"
"replace with one"

effbiae
New Contributor
or an "i don't have ssr" soln:

q)x where 2>+':[(" "=)["replace     with    one"]]
"replace with one"

Recently came up on http://stackoverflow.com/questions/36587117/

q)s:"replace     with    one"
q)\ts {spaces:" "=x; x where not spaces and prev[spaces]}s
0 2448j
q)\ts {x where(or)':[not null x]}s
0 1600j
q)\ts ssr[;"  ";" "] over s
0 1216j
q)\ts x where 2>+':[(" "=)[s]]
0 1264j

q)\ts (" "sv"  "vs)/[s]
0 864j



Thanks,

Connor