Roman Kabiev
Roman Kabiev

Reputation: 11

postgresql: regexp_split_to_table - how to split text by delimiters

I need to split my text to table by delimiters '<=' and '=>', for example

select regexp_split_to_table('kik plz <= p1 => and <= p2 => too. A say <=p1 =>','regexp');

The result must be:

table:
--------------
1 | 'kik plz '
2 | '<= p1 =>'
3 | ' and '
4 | <= p2 =>
5 | ' too. A say '
6 | '<=p1 =>'

I think the answer is in positional patterns, but my skills are not enough.

select regexp_split_to_table('kik plz <= p1 => and <= p2 => too. A say <=p1 =>', '((\s)(?=<=))|((\s)(?!=>))') 

This returns the wrong result.

Upvotes: 0

Views: 1769

Answers (1)

Roman Kabiev
Roman Kabiev

Reputation: 11

select regexp_split_to_table(
    replace(
        replace('kik plz<= p1 =>and<= p2 =>too. A say <=p1 =>',  '<=',  E'\001<=')
    , '=>', E'=>\001')
, E'\001');

Upvotes: 1

Related Questions