formatting - RegEx to add commas and Single quotes -
thanks in advance help... have panel of information in following format:
acmh admit xxx acsu admit xxx sub-acute (tcu) adopt adoption ama against medical advice apsy admit cmh psychiatric unit cancel cancelled service canceler cancelled er canceltri cancelled triage exp expired
i need format panel this:
'acmh','admit xxx' 'acsu','admit xxx sub-acute (tcu)' 'adopt','adoption' 'ama','against medical advice' 'apsy','admit cmh psychiatric unit' 'cancel','cancelled service' 'canceler','cancelled er' 'canceltri','cancelled triage' 'exp','expired'
my colleague recommended regex replacement. appropriate strategy? if can provide me guidelines on how achieve this.
regex experience limited (close non-existant) explanations appreciated.
this information being inserted sql server database. have program read .csv file single quotes , pretty insert table.
i.e....
insert mytable ( codevalue, description) values ('acmh', 'admit xxx')
we have never been given panel wondering if possible achieve.
you can replace
^(\s+)\s+(.*?)\s*$
with
'$1','$2'
explanation:
^
matches beginning of line(\s+)
matches non-empty sequence of non-space characters. parentheses around puts in capture group 1\s+
matches spaces after first field(.*?)
non-greedy match of sequence of characters, , parentheses put match in capture group 2\s*
matches spaces @ ends of lines. did won't included in preceding capture group, remove trailing whitespace in input data.$
matches end of line
in replacement $1
, $2
repaced contents of capture groups.
Comments
Post a Comment