regex - how to split a string into an array using last occurrence of '-' using regexp_split_to_array.


Keywords:regex 


Question: 

I have a string look like "P-263-CA" and i want to split it using '-' character using last occurrence so that the out put will be {P-263,CA} using regexp_split_to_array . any idea ?


1 Answer: 

select regexp_split_to_array('P-263-CA', '(-)(?!.*-)');

 regexp_split_to_array 
-----------------------
 {P-263,CA}
(1 row)

Explanation