```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| │
└─────────────────┘
```
size_t start_pos = 0;
        bool is_first_match = true;
        bool is_start_pos_added_one = false;
        while (start_pos < static_cast<size_t>(input.length()))
        {
            
            bool can_finish_current_string = false;
            if (searcher.Match(input, start_pos, input.length(), re2_st::RE2::Anchor::UNANCHORED, matches, num_captures))
            {
                if (is_start_pos_added_one)
                    start_pos -= 1;
                const auto & match = matches[0];
                size_t bytes_to_copy = (match.data() - input.data()) - start_pos;
                
                res_data.resize(res_data.size() + bytes_to_copy);
                memcpySmallAllowReadWriteOverflow15(&res_data[res_offset], input.data() + start_pos, bytes_to_copy);
                res_offset += bytes_to_copy;
                start_pos += bytes_to_copy + match.length();
                
                if (is_first_match && match.length() == 0 && !replace_one && input.length() > 1)
                {
                    start_pos += 1;
                    is_start_pos_added_one = true;
                }
                
                for (const auto & it : instructions)
                {
                    if (it.first >= 0)
                    {
                        res_data.resize(res_data.size() + matches[it.first].length());