Friday, 15 April 2011

regex - Query to replace a string with matching pattern -



regex - Query to replace a string with matching pattern -

i have string below.

'comp' "computer",'ms' "mouse" ,'keybr' "keyboard",'mont' "monitor",

is possible write query result as

'comp' ,'ms' ,'keybr' ,'mont' ,

i can replace string "as" empty string using replace query. how remove string within double quote?

can help me doing this? in advance.

select replace( regexp_replace('''comp'' "computer"' , '(".*")' ,null) ,' ' ,null) dual

the regex '(".*")' selects text whatever in double quotes.

edit:

the regex replaced entire length of matching pattern. so, might need tokenise string first using comma delimiter , apply regex. later bring together it.(listagg)

with str_tab(str1, rn) (select regexp_substr(str, '[^,]+', 1, level), -- delimts level (select '''comp'' "computer",''comp'' "computer"' str dual) tab connect level <= length(str) - length(replace(str, ',')) + 1) select listagg(replace( regexp_replace(str1 , '(".*")' ,null) ,' ' ,null), ',') within grouping (order rn) new_text str_tab;

edit2:

a cleaner approach @eatapeach

with x(y) ( select q'<'comp' "computer",'ms' "mouse" ,'keybr' "keyboard",'mont' "monitor",>' dual ) select y, regexp_replace(y, 'as ".*?"' ,null) x;

regex oracle replace

No comments:

Post a Comment