QGIS Regex – How to Split a Text String with Each Character as an Element of an Array Using QGIS Expressions and Regular Expressions

expressionqgisregular expressionstring

I want to use QGIS expressions to split up a text string to an array with each element representing one character of the input's string, like:

input: mytext -> ouput: [ 'm', 'y', 't', 'e', 'x', 't' ]

I tried regular expressions with regexp_matches() function, but was not able to achieve it but partially:

regexp_matches( ('mytext'),'(.+)(.)(.+)') -> [ 'myte', 'x', 't' ]

I could, of course, use the same numer of capturing group (.) as the input has characters. But the goal is an expression that works with any number of characters. How to achieve that?

Best Answer

Don't think so complicated ;)

array_remove_all(string_to_array('mytext',''),'')

Result: [ 'm', 'y', 't', 'e', 'x', 't' ].

For some reason string_to_array() adds empty stuff ('') to the array as first and last position, so we remove it with array_remove_all()