Reputation: 27
I have got few files which are by separated by multiple spaces, I wrote a regex which would remove all the white spaces but not able to add the delimiter after end of every file. Can you please assist?
select regexp_replace(file,'[[:space:]]',',') from test_table
where id in ('1','2');
The above select only works if there is a single space between files.
sample data
filename : |
---|
this is the file sent.today .msg required mail.msg |
aa.docx bb.docx cc.pdf |
required output (delimiter added(,) after each file
filename : |
---|
this is the file sent.today .msg, required mail.msg |
aa.docx, bb.docx, cc.pdf |
Upvotes: 0
Views: 137
Reputation: 162
regexp_replace(data,'(\.[a-zA-Z]+) ([a-zA-Z])','\1, \2')
Each group (behind parenthesis) in the regex will be matched to their number in the resulting string (ie \1 and \2)
Note also that this regex does not work for file type containing a number.
Upvotes: 1
Reputation:
You explained that you can consolidate whitespace to single spaces. That has to be done separately from the "adding of commas" anyway, so let's say that's done.
You need a second REGEXP pass on the result of the first. Something like this:
regexp_replace(result_of_first_pass, '\.(docx|pdf|msg) ', '.\1, ')
Upvotes: 1