Functions for Splitting and Merging Strings and Arrays

splitByChar(separator, s)

Splits a string into substrings separated by a specified character. It uses a constant string separator which consisting of exactly one character.
Returns an array of selected substrings. Empty substrings may be selected if the separator occurs at the beginning or end of the string, or if there are multiple consecutive separators.

Syntax

  1. splitByChar(<separator>, <s>)

Parameters

  • separator — The separator which should contain exactly one character. String.
  • s — The string to split. String.

Returned value(s)

Returns an array of selected substrings. Empty substrings may be selected when:

  • A separator occurs at the beginning or end of the string;
  • There are multiple consecutive separators;
  • The original string s is empty.

Type: Array of String.

Example

  1. SELECT splitByChar(',', '1,2,3,abcde')
  1. ┌─splitByChar(',', '1,2,3,abcde')─┐
  2. ['1','2','3','abcde']
  3. └─────────────────────────────────┘

splitByString(separator, s)

Splits a string into substrings separated by a string. It uses a constant string separator of multiple characters as the separator. If the string separator is empty, it will split the string s into an array of single characters.

Syntax

  1. splitByString(<separator>, <s>)

Parameters

  • separator — The separator. String.
  • s — The string to split. String.

Returned value(s)

Returns an array of selected substrings. Empty substrings may be selected when:

Type: Array of String.

  • A non-empty separator occurs at the beginning or end of the string;
  • There are multiple consecutive non-empty separators;
  • The original string s is empty while the separator is not empty.

Example

  1. SELECT splitByString(', ', '1, 2 3, 4,5, abcde')
  1. ┌─splitByString(', ', '1, 2 3, 4,5, abcde')─┐
  2. ['1','2 3','4,5','abcde']
  3. └───────────────────────────────────────────┘
  1. SELECT splitByString('', 'abcde')
  1. ┌─splitByString('', 'abcde')─┐
  2. ['a','b','c','d','e']
  3. └────────────────────────────┘

arrayStringConcat(arr[, separator])

Concatenates the strings listed in the array with the separator.’separator’ is an optional parameter: a constant string, set to an empty string by default.
Returns the string.

alphaTokens(s)

Selects substrings of consecutive bytes from the ranges a-z and A-Z.Returns an array of substrings.

Example

  1. SELECT alphaTokens('abca1abc')
  1. ┌─alphaTokens('abca1abc')─┐
  2. ['abca','abc']
  3. └─────────────────────────┘

extractAllGroups(text, regexp)

Extracts all groups from non-overlapping substrings matched by a regular expression.

Syntax

  1. extractAllGroups(text, regexp)

Parameters

Returned values

  • If the function finds at least one matching group, it returns Array(Array(String)) column, clustered by group_id (1 to N, where N is number of capturing groups in regexp).

  • If there is no matching group, returns an empty array.

Type: Array.

Example

Query:

  1. SELECT extractAllGroups('abc=123, 8="hkl"', '("[^"]+"|\\w+)=("[^"]+"|\\w+)');

Result:

  1. ┌─extractAllGroups('abc=123, 8="hkl"', '("[^"]+"|\\w+)=("[^"]+"|\\w+)')─┐
  2. [['abc','123'],['8','"hkl"']]
  3. └───────────────────────────────────────────────────────────────────────┘

Original article