Regex CSV Replace Comma between Quotes

时光总嘲笑我的痴心妄想 提交于 2019-12-11 06:06:19

问题


We have for example a string like this:

"COURSE",247,"28/4/2016 12:53 Europe/Brussels",1,"Verschil tussen merk, product en leveranciersverantwoordelijke NL","Active Enro"

The Goal is to replace the comma between "merk, product" and to keep the comma like "," and ", & ," so we can split the file correctly.

Any suggestions?

Kind regards


回答1:


First of all, you should check Understanding CSV files and their handling in ABAP article.

For a one-time job, you can use this regex (but note that with longer strings, it may not work well, use it as a means of last resort):

,(?!(?:[^"]*"[^"]*")*[^"]*$)

See the regex demo

Pattern details:

  • , - a comma that...
  • (?! - is not followed with....
    • (?: -
      • [^"]* - zero or more chars other than "
      • " - a double quote
      • [^"]*" - see above
    • )* - zero or more sequences of the above grouped patterns
    • [^"]* - zero or more chars other than "
    • $ - end of string
  • ) - end of negative lookahead



回答2:


I've found a better solution than Regex, by using the class CL_RSDA_CSV_CONVERTER. No need of reinventing the wheel.

See code below:

TYPES: BEGIN OF ttab,
         rec(1000) TYPE c,
       END OF ttab.
TYPES: BEGIN OF tdat,
         userid(100)                TYPE c,
         activeuser(100)            TYPE c,
         firstname(100)             TYPE c,
         lastname(100)              TYPE c,
         middlename(100)            TYPE c,
         supervisor(100)            TYPE c,
         supervisor_firstname(100)  TYPE c,
         supervisor_lastname(100)   TYPE c,
         supervisor_middle(100)     TYPE c,
         scheduled_offering_id(100) TYPE c,
         description(100)           TYPE c,
         domain(100)                TYPE c,
         registration(100)          TYPE c,
         current_registration(100)  TYPE c,
         max_registration(100)      TYPE c,
         item_type(100)             TYPE c,
         item_id(100)               TYPE c,
         item_revision_date(100)    TYPE c,
         revision_number(100)       TYPE c,
         title(100)                 TYPE c,
         status(100)                TYPE c,
         start_date(100)            TYPE c,
         end_date(100)              TYPE c,
         location(100)              TYPE c,
         instructor_fistname(100)   TYPE c,
         instructor_lastname(100)   TYPE c,
         instructor_middlename(100) TYPE c,
         column_number(100)         TYPE c,
         label(100)                 TYPE c,
         value(100)                 TYPE c,
         description2(100)          TYPE c,
         start_date_short(100)      TYPE c,
         begda                      TYPE begda,
         start_time(100)            TYPE c,
         start_time_24_hour(100)    TYPE c,
         start_12_hour_type(100)    TYPE c,
         start_timezone(100)        TYPE c,
         end_date_short(100)        TYPE c,
         endda                      TYPE endda,
         end_time(100)              TYPE c,
         end_time_24_hour(100)      TYPE c,
         end_12_hour_type(100)      TYPE c,
         end_timezone(100)          TYPE c,
         pernr                      TYPE pernr_d,
       END OF tdat.

CONSTANTS: co_delete           TYPE pspar-actio VALUE 'DEL',
           co_attendance       TYPE string VALUE '2002',
           co_att_prelp        TYPE prelp-infty VALUE '2002',
           co_att_subty        TYPE string VALUE '3000'.

DATA:
  itab                 TYPE TABLE OF ttab WITH HEADER LINE,
  idat                 TYPE TABLE OF tdat WITH HEADER LINE,
  lw_idat              LIKE LINE OF idat,
  lw_found_training    LIKE LINE OF idat,
  file_str             TYPE string,
  lv_uname             TYPE syuname,
  lo_person            TYPE REF TO zhr_cl_pa_person,
  lv_input_time        TYPE tims,
  lv_output_time       TYPE tims,
  lv_day(2)            TYPE c,
  lv_month(2)          TYPE c,
  lv_year(4)           TYPE c,
  lv_time(6)           TYPE c,
  lv_abap_date         TYPE string,
  lv_lock_return       LIKE bapireturn1,
  ls_attendance        LIKE bapihrabsatt_in,
  lt_attendance_output TYPE TABLE OF bapiret2,
  ls_return            LIKE bapireturn,
  ls_return1           LIKE bapireturn1,
  lt_absatt_data       TYPE TABLE OF pprop,
  lw_absatt_data       LIKE LINE OF lt_absatt_data,
  lt_pa2002            TYPE TABLE OF pa2002,
  lw_pa2002            LIKE LINE OF lt_pa2002,
  lw_msg               TYPE bapireturn1,
  lt_p2002             TYPE TABLE OF p2002,
  lw_p2002             LIKE LINE OF lt_p2002,
  lc_pgmid             TYPE old_prog VALUE 'ZKA_TEXT_UPDATE',
  lr_upd_cluster       TYPE REF TO cl_hrpa_text_cluster,
  ls_text              TYPE hrpad_text,
  ls_pskey             TYPE pskey,
  lt_text_194          TYPE hrpad_text_tab,
  lv_text              TYPE string,
  lo_ref               TYPE REF TO cx_hrpa_invalid_parameter,
  lw_struct            TYPE tdat,
  lo_csv               TYPE REF TO cl_rsda_csv_converter.

CALL METHOD cl_rsda_csv_converter=>create
  RECEIVING
    r_r_conv = lo_csv.
CREATE OBJECT lr_upd_cluster.

*--------------------------------------------------*
* selection screen design
*-------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK selection1 WITH FRAME.
PARAMETERS: p_file TYPE localfile.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 4(51) text-002.
PARAMETERS p_futatt AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 4(51) text-001.
PARAMETERS p_active AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK selection1.

*--------------------------------------------------*
* at selection screen for field
*-------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      static    = 'X'
    CHANGING
      file_name = p_file.

*--------------------------------------------------*
* start of selection
*-------------------------------------------------*
START-OF-SELECTION.
  file_str = p_file.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = file_str
    TABLES
      data_tab                = itab
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.

*--------------------------------------------------------------------*
* Delete file headers
*--------------------------------------------------------------------*
  DELETE itab INDEX 1.

*--------------------------------------------------*
* process and display output
*-------------------------------------------------*

  LOOP AT itab  .
    CLEAR idat.

    CALL METHOD lo_csv->csv_to_structure
      EXPORTING
        i_data   = itab-rec
      IMPORTING
        e_s_data = lw_struct.

    MOVE-CORRESPONDING lw_struct TO idat.

    APPEND idat.
  ENDLOOP.



回答3:


  1. Read the file using a CSV reader.
  2. Replace the commas in each field.
  3. Write the file using a CSV writer.

You don’t need regular expressions for this task.



来源:https://stackoverflow.com/questions/38300941/regex-csv-replace-comma-between-quotes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!