ss, ssr¶
String search and replace
ss¶
String search
x ss y ss[x;y]
Where
xis a stringyis a pattern as a char atom or string (ssdoesn't support*as a wildcard)
returns a long vector of the positions within x of substrings that match pattern y.
q)"We the people of the United States" ss "the"
3 17
q)s:"toronto ontario"
q)s ss "ont"
3 8
q)s ss "[ir]o"
2 13
q)s ss "t?r"
0 10
Matches do not overlap: after a match, the search resumes at the end of the matched substring.
q)"aaaa" ss "aa"
0 2
ssr¶
String search and replace
ssr[x;y;z]
Where
xis a stringyis a pattern as a char atom or string (ssrdoesn't support*as a wildcard)zis a string or a function
returns x with each substring matching y replaced by:
zifzis a stringz[Y]ifzis a function, whereYis the matched substring
q)s:"toronto ontario"
q)ssr[s;"ont";"x"] / replace "ont" by "x"
"torxo xario"
q)ssr[s;"t?r";upper] / replace matches by their uppercase
"TORonto onTARio"
Perl-compatible regular expressions¶
ss and ssr match each pattern character to exactly one character of x. For full regular expressions, use the PCRE2 module. pcre2.match returns each matched substring with its start and end positions, and pcre2.replace supports references to captured groups in the replacement.
q)"abc 123 de 45" ss "[0-9]"
4 5 6 11 12
q)pcre2:use`kx.fusion:pcre2
q)pcre2.match["[0-9]+";"abc 123 de 45";::]
x0| "123" 4 7 "45" 11 13
q)pcre2.replace["([a-z]+)@([a-z]+)";"me@host";"$2 at $1";::]
"host at me"