REGEX_TOO_COMPLEX error when parsing regex expression

Viewed 210

I need to split the CSV file at commas, but the problem is that file can contain commas inside fields. So for an example:

one,two,tree,"four,five","six,seven".

It uses double quotes to escape, but I could not solve it. I tried to use something like this with this regex, but I got an error: REGEX_TOO_COMPLEX.

    data: lv_sep     type string,
      lv_rep_pat type string.
    data(lv_row) = iv_row.
"Define a separator to replace commas in double quotes
lv_sep = cl_abap_conv_in_ce=>uccpi( uccp = 10 ).
concatenate '$1$2' lv_sep into lv_rep_pat.
"replace all commas that are separator with the new separator
replace all occurrences of regex '(?:"((?:""|[^"]+)+)"|([^,]*))(?:,|$)' in lv_row with lv_rep_pat.

split lv_row at lv_sep into table rt_cells.
2 Answers

I never ever touched ABAP, so please see this as pseudo code

I'd recommend using a non-regex solution here:

data: checkedOffsetComma type i,
checkedOffsetQuotes type i,
baseOffset type i,
testString type string value 'val1, "val2, val21", val3'.

LOOP AT SomeFancyConditionYouDefine.
    checkedOffsetComma = baseOffset.
    checkedOffsetQuotes = baseOffset.
    find FIRST OCCURRENCE OF ','(or end of line here) in testString match OFFSET checkedOffsetComma.
    write checkedOffsetComma.
    find FIRST OCCURRENCE OF '"' in testString match OFFSET checkedOffsetQuotes.
    write checkedOffsetQuotes.
    
    *if the next comma is closer than the next quotes
    IF checkedOffsetComma < checkedOffsetQuotes.
        REPLACE SECTION checkedOffsetComma 1 OF ',' WITH lv_rep_pat.
        baseOffset = checkedOffsetComma.
    ELSE.
        *if we found  quotes, we go to the next quotes afterwards and then continue as before after that position
        find FIRST OCCURRENCE OF '"' in testString match OFFSET checkedOffsetQuotes.
        write baseOffset.
    ENDIF.
ENDLOOP.

This assumes that there are no quotes in quotes thingies. Didn't test, didn't validate in any way. I'd be happy if this at least partly compiles :)

You must use this Regex => ,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)

DATA: lv_sep     TYPE string,
      lv_rep_pat TYPE string.
DATA(lv_row) = 'one,two,tree,"four,five","six,seven"'.
"Define a separator to replace commas in double quotes
lv_sep = cl_abap_conv_in_ce=>uccpi( uccp = 10 ).
CONCATENATE '$1$2' lv_sep INTO lv_rep_pat.
"replace all commas that are separator with the new separator
REPLACE ALL OCCURRENCES OF REGEX ',(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)' IN lv_row WITH lv_rep_pat.

SPLIT lv_row AT lv_sep INTO TABLE data(rt_cells).

LOOP AT rt_cells into data(cells).
  WRITE cells.
  SKIP.

ENDLOOP.

Testing output

Related