```sql
SELECT trim(BOTH ', ' FROM '5935,5998,6014, ')||'|' x

SELECT concat(replaceRegexpAll('5935,5998,6014, ', concat('^[', regexpQuoteMeta(', '), ']*|[', regexpQuoteMeta(', '), ']*$'), ''), '|') AS x

┌─x─────────────────┐
│ 5935,5998,6014, | │
└───────────────────┘

psql> SELECT trim(BOTH ', ' FROM '5935,5998,6014, ')||'|'
5935,5998,6014|

```
http://sqlfiddle.com/#!17/0a28f/406

though trailing
```sql
SELECT trim(trailing ', ' FROM '5935,5998,6014, ')||'|' x
┌─x───────────────┐
│ 5935,5998,6014| │
└─────────────────┘
```
memcpy(&res_data[res_offset], matches[it.first].data(), matches[it.first].length());
                        res_offset += matches[it.first].length();
                    }
                    else
                    {
                        res_data.resize(res_data.size() + it.second.size());
                        memcpy(&res_data[res_offset], it.second.data(), it.second.size());
                        res_offset += it.second.size();
                    }
                }
                if (replace_one || (!is_first_match && match.length() == 0))
                    can_finish_current_string = true;
                is_first_match = false;
            }
            else
                can_finish_current_string = true;
            
            if (can_finish_current_string)
            {
                res_data.resize(res_data.size() + input.length() - start_pos);
                memcpySmallAllowReadWriteOverflow15(&res_data[res_offset], input.data() + start_pos, input.length() - start_pos);
                res_offset += input.length() - start_pos;
                start_pos = input.length();
            }
        }
        res_data.resize(res_data.size() + 1);
        res_data[res_offset] = 0;
        ++res_offset;
    }
    static void vector(