【问题标题】:REGEX_TOO_COMPLEX error when parsing regex expression解析正则表达式时出现 REGEX_TOO_COMPLEX 错误
【发布时间】:2022-01-20 13:13:05
【问题描述】:

我需要以逗号分隔 CSV 文件,但问题是文件可以在字段内包含逗号。举个例子:

一、二、树、“四、五”、“六、七”。

它使用双引号来转义,但我无法解决它。 我试图在这个正则表达式中使用类似的东西,但我得到了一个错误: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.

【问题讨论】:

  • 请参阅softwareengineering.stackexchange.com/q/166454/203609 以获得一些非常完整的答案。我并不是说他们对您所面临的情况很准确(“如何使用转义逗号使其正常工作”的解决方案可能隐藏在几层之下......寻找“引用的价值” "),但我认为那里给出的关于你可以/应该用正则表达式做什么的限制的警告可能值得你花时间。我的总体建议是,除了最简单的 CSV 数据之外,您尝试使用其他工具(例如 Python“csv”库)。
  • (?<=,")(?:\w[,]?)+\K|, 有帮助吗?这是regex demo。它假定PCRE 风味。
  • ABAP 7.55 之前,RegEx 只支持很差很慢的 POSIX 版本,REGEX_TOO_COMPLEX 经常发生(可能是由于解析文本的数量,更好的 RegEx 可以解决)。从 ABAP 7.55 开始支持 PCRE,具有更强大的引擎和功能。如果你想支持完整的 CSV 语法(包括字段内的换行符),我不推荐 RegEx。
  • 使用您的代码和 CSV 行,它可以在 ABAP 7.52 中使用。请发帖minimal reproducible example。谢谢。

标签: regex abap


【解决方案1】:

我从来没有接触过ABAP,所以请把它看成伪代码

我建议在这里使用非正则表达式解决方案:

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.

这假设引号中没有引号。没有测试,没有以任何方式验证。如果这至少部分编译,我会很高兴:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多