Good day.

The task is as follows: there is a line like 'Channel aps-61.ds0Grp-1.1.5.3.1: 0.0'. It is necessary in postgres to convert it (probably using regexp) to the form: aps-61-VC4-1-TU-1.5.3. Those. from 1.1.5.3.1: 0.0 the first number is taken and is written after VC4-, and the next three numbers 1.5.3 are written as TU-1.5.3. Subsequent numbers recline.

I tried to solve this problem like this:

SELECT regexp_matches('Channel aps-61.ds0Grp-1.1.5.3.1:0.0', 'aps-[0-9]{2}') 

But the thought has not advanced further; regular expressions are given with difficulty. You can, of course, probably connect the python module to postgres and create a function, but you can get by with less blood. If you have ideas, I will be grateful.

  • You need regexp_replace () - Mike
  • Yes, I tried it too, but I didn’t figure it out. I figured out how to find the first entry (aps-61), and how the rest and where to substitute into the template does not come out a stone flower. - Ruslan
  • regexp_replace(x,'.*?(aps-\d+).*?-(\d+)\.(\d+\.\d+\.\d+).*$','\1-VC4-\2-TU-\3') regex101.com/r/tAClFJ/1 - Mike
  • Thank you so much, it works! And for the link special thanks. - Ruslan

1 answer 1

I don’t know what syntax is in postgresql, but the regulars would be:

 preg_replace("/.*(aps-\d{2})[^-]*-(\d)\.(\d\.\d\.\d).*/", "$1-VC4-$2-TU-$3", $input_lines); 

http://www.phpliveregex.com/p/hVa