Reputation: 77
I am using regexp_split_to_array
to split a text in words by cutting of white space as following:
select regexp_split_to_array(' this is just me ', '\s+');
This gives:
{"",this,is,just,me,""}
I want to have:
{this,is,just,me}
Is there a way to remove the leading and trailing white space when applying regexp_split_to_array
?
Upvotes: 1
Views: 398
Reputation: 11
Try using trim
on the string before passing it to regex_split_to_array
function:
select regexp_split_to_array(trim(' this is just me '), '\s+');
Upvotes: 0
Reputation: 626689
I suggest the inverse approach: match any char(s) other than whitespace with \S+
regex pattern:
select regexp_matches(' this is just me ', '\S+', 'g')
See the online demo. Note that 'g'
argument will enable multiple matching.
Use unnest
on the regexp matches to expand an array to a set of rows if necessary.
Upvotes: 1
Reputation: 4754
This should do it:
SELECT REGEXP_SPLIT_TO_ARRAY(TRIM(' this is just me '), '\s+');
TRIM
with no parameters will remove both leading and trailing whitespace from a string.
Upvotes: 0