Sql Regular Expression To Split A Column (string) To Multiple Rows Based On Delimiter '/n'
Solution 1:
you need to use class [[:cntrl:]] and '[^/n]+' is not syntactically good either.
the escape char is '\' and you cannot use [] to "wrap" special chars, you need to use () instead.(that is grouping)
if you want to ignore CR (e.g.'\n') , use [^[:cntrl:]] in the sec param in the regexp_substr
more help: http://psoug.org/snippet/Regular-Expressions--Regexp-Cheat-Sheet_856.htm
Solution 2:
Assumption
/n is supposed to mean \n to match a newline ( strictly [Posix] speaking a LF character (hex x0a) ).
If this assumption is wrong, use (^|/n)(([^/]|/+[^n])+) as your regex and extract the part of interest using regexp_substr(attribute_1,'(^|/n)(([^/]|/+[^n])+)', 1, column_value, '', 2).
Solution
You cannot specify control characters in escape syntax within character classes. Using the posix character class [:cntrl:] works but suffers from the other characters included; for practical purposes, TAB ( #x09 ) might be a nuisance.
However, you can specify all characters in a regex character class composing the pattern string from literals and calls to the chr function:
-- ...
'3243243242342342'||chr(13)||chr(10)||'12131212312'||chr(13)||chr(10)||'123131232'||chr(13)||chr(10) as attribute_1,
'test value'||chr(13)||chr(10)||'neenu not'||chr(13)||chr(10)||'honey'as attribute_2
-- ...
regexp_substr(attribute_1,'[^'||chr(13)||chr(10)||']+', 1, column_value),
regexp_substr(attribute_2,'[^'||chr(13)||chr(10)||']+', 1, column_value)
-- ...
You may want to check out the following test queries in sqlplus (the cr/lfs are part of the literals; copy into a text editor, check that the cr/lfs are preserved, re-insert if not, drop the result in sqlplus):
select regexp_substr('adda
yxcv','[^'||CHR(10)||CHR(13)||']+', 1, 2) from dual;
select regexp_substr('ad'||CHR(9)||'da
yxcv','[^[:cntrl:]]+', 1, 2) from dual;
Solution 3:
with test as (select 'ABC' || chr(13) || 'DEF' || chr(13) || 'GHI' || chr(13) || 'JKL' || chr(13) || 'MNO'strfrom dual)
select regexp_substr (str, '[^' || chr(13) || ']+', 1, rownum) split
from test
connect by level <= length (regexp_replace (str, '[^' || chr(13) || ']+')) + 1Solution 4:
First choice would be to fix the data model as data stored this way is not optimal. At any rate, try this version with some more test data. I tweaked the regex's:
WITH sample AS
( SELECT101AS id,
'Name' test,
'3243243242342342/n12131212312/n123131232/n'as attribute_1,
'test value/nneenu not/nhoney'as attribute_2
FROM DUAL
)
-- end of sample dataSELECT id,
test,
regexp_substr(attribute_1,'(.*?)(/n|$)', 1, column_value, NULL, 1),
regexp_substr(attribute_2,'(.*?)(/n|$)', 1, column_value, NULL, 1)
FROM sample,
TABLE(
CAST(
MULTISET(SELECT LEVEL
FROM dual
--CONNECT BY LEVEL <= LENGTH(attribute_1) - LENGTH(replace(attribute_1, '/n')) + 1-- Counts substrings ending with the delimiter.CONNECTBY LEVEL <= REGEXP_COUNT(attribute_1, '.*?/n')
) AS sys.OdciNumberList
)
)
WHERE regexp_substr(attribute_1,'(.*?)(/n|$)', 1, column_value, NULL, 1) ISNOTNULL/
Post a Comment for "Sql Regular Expression To Split A Column (string) To Multiple Rows Based On Delimiter '/n'"