I have below dataset,
1. '{SHEET,2730377,SHEET,5708283,DATA,3015937}'
2. '{SHEET,2730377,SHEET,5708283,DATA,3015937,DATA,0010965}'
3. '{SHEET,5708283,DATA,3015937,DATA,0010965}'
3. '{SHEET,5708283,DATA,3015937}'
I need the result as,
1. {DATA,3015937}
2. {DATA,3015937,DATA,0010965}
3. {DATA,3015937,DATA,0010965}
4. {DATA,3015937}
Basically I wanted to replace SHEET followed by 7 digit number and extract only DATA followed by 7 digit number.
I tried using regexp_replace('{SHEET,2730377,SHEET,5708283,DATA,3015937}', 'SHEET,(ddddddd),', '' )
the result of this is {SHEET,5708283,DATA,3015937}
, as well as using similar to [0-9][0-9][0-9][0-9][0-9][0-9][0-9]
Finding difficulties to get the accurate result, any suggestion would be appreciated
2
Answers
If, as in the examples,
there are one or "DATA" pairs that are contiguous and are followed by
"}'
at the end of the string; andthere is at least one "STREET" pair that precedes the first "DATA" pair,
then you may convert matches of the following regular expression to empty strings:
Demo
I understand Postgres supports lookaheads.
The expression can be broken down as follows.
Instead of
REGEXP_REPLACE
ing what you want to remove, justSELECT
what you want to extract as follows (all of the code below is available on the fiddle here):populate:
and then we run the following:
Result:
Et voilĂ !