I am trying to use Oracle's REGEXP_SUBSTR to select fields in a string.
Example:
this,,,is,,,an,,,example
Solution:
DECLARE
field1 VARCHAR2(4000);
field2 VARCHAR2(4000);
field3 VARCHAR2(4000);
field4 VARCHAR2(4000);
separator VARCHAR2(300) := ',,,';
lineToParse VARCHAR2(4000) := 'this,,,is,,,an,,,example';
BEGIN
SELECT REGEXP_SUBSTR(lineToParse, '[^' || separator || ']+', 1, 1) AS part_1, REGEXP_SUBSTR(lineToParse, '[^' || separator || ']+', 1, 2) AS part_2, REGEXP_SUBSTR(lineToParse, '[^' || separator || ']+', 1, 3) AS part_3, REGEXP_SUBSTR(lineToParse, '[^' || separator || ']+', 1, 4) AS part_4
INTO field1, field2, field3, field4
FROM DUAL;
DBMS_OUTPUT.PUT_LINE('Field 1: ' || field1);
DBMS_OUTPUT.PUT_LINE('Field 2: ' || field2);
DBMS_OUTPUT.PUT_LINE('Field 3: ' || field3);
DBMS_OUTPUT.PUT_LINE('Field 4: ' || field4);
END;
This works perfectly for the line above, generating:
Field 1: this
Field 2: is
Field 3: an
Field 4: example
However for the below line, it doesn't
this,,,is, a perfectly fine,,,new,,, line
This is because the second capture group should be: "is, a perfectly fine" but ends up being "is".
Output is:
Field 1: this
Field 2: is
Field 3: a perfectly fine
Field 4: new
Reason is that the regex I am using:
[^,,,]+
Is capturing any of the characters that follow ^ instead of the sequence.
How can I adjust my regex so that the entire sequence is captured?
This needs to be compatible with Oracle 11g.