-1

I have a query that returns a column with a string I need to clean up. I would like to remove the portion of this string that starts with ",id:" and ends right before ")". The string could have many occurrences of the substring to be removed. The alphanumeric substring that follows "id:" is always the same length.

This is a sample string:

(label:Boston,include:true,id:9fc53def-0b9d-45cb-8f12-d42c3ca70ab2),
(label:Cambridge,include:true,id:dda3d6d7-f9d1-45ac-ac6e-5866b356966e)

This is the string I would like to see at the end:

(label:Boston,include:true),
(label:Cambridge,include:true)
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Komsinica
  • 1
  • 3

3 Answers3

0
SELECT regexp_replace(
          '(label:Cambridge,include:true,id:dda3d6d7-f9d1-45ac-ac6e-5866b356966e)',
          ',?\mid:.{36}',  -- pattern
          '',            -- replacement
          'g'            -- flag: replace multiple occurrences
       );
         regexp_replace         
════════════════════════════════
 (label:Cambridge,include:true)
(1 row)

Explaining the pattern:

  • ,? matches a comma, if there is one

  • \m matches the beginning of a word

  • .{36} matches 36 arbitrary characters

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
0

try this it will hepls you......

select SUBSTRING('label:Boston,include:true,id:9fc53def-0b9d-45cb-8f12-d42c3ca70ab2',1,LEN('label:Boston,include:true,id:9fc53def-0b9d-45cb-8f12-d42c3ca70ab2')-40) as stringaa;
PrA-patel
  • 99
  • 1
  • 2
0

starts with ",id:" and ends right before ")"

SELECT regexp_replace(string, ',id:[0-9a-f-]{36}\)', ')', 'g');

Neither comma nor closing parenthesis are optional in your description.

Or with an even more rigid pattern to match standard Postgres UUID format as described in the manual:

SELECT regexp_replace(string
                    , ',id:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\)'
                    , ')', 'g');

Related:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228