title: How to remove repeated commas and trim from each end using REGEXP_REPLACE?
body: <p>I need to concatenate several fields together, which may or may not be null. I could potentially end up with a string like: ',,c,,e,,' which I actually want to show as 'c,e'.</p>
  
  <p>I can get this via a combination of <code>regexp_replace</code> and <code>trim</code>:</p>
  
  <pre><code>with sd as (select 'a,b,c' str from dual union all
              select 'a' str from dual union all
              select null str from dual union all
              select 'a,,,d' from dual union all
              select 'a,,,,e,f,,'from dual union all
              select ',,,d,,f,g,,'from dual)
  select str,
         regexp_replace(str, '(,)+', '\1') new_str,
         trim(both ',' from regexp_replace(str, '(,)+', '\1')) trimmed_new_str
  from   sd;
  
  STR         NEW_STR     TRIMMED_NEW_STR
  ----------- ----------- ---------------
  a,b,c       a,b,c       a,b,c          
  a           a           a              
  
  a,,,d       a,d         a,d            
  a,,,,e,f,,  a,e,f,      a,e,f          
  ,,,d,,f,g,, ,d,f,g,     d,f,g  
  </code></pre>
  
  <p>but I feel like it ought to be doable in a single <code>regexp_replace</code> only I can't work out for the life of me how it could be done!</p>
  
  <p>Is it possible? If so, how?</p>