Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ABAP REGEX to validate date format

0 Likes
3,963

Hello,

I have a requirement to validate date format of BSEG-ZUONR.

If the date format is not equal to YYYY/MM/DD, we need to print error message saying 'date format is incorrect'.

Can anyone let me know how this can be achieved? can we write a regex?

Thanks in advance.

Regards,

Tejas.

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
3,883

If you are not in the mood to use regex anymore, because you have realized that to insure actual dates, a regex is not the way to do it, you can try something like

  TRY.
      cl_abap_datfm=>conv_date_ext_to_int(
        EXPORTING
          im_datext    = bseg-zuonr
          im_datfmdes  = '5'
  IMPORTING
    ex_datint    = DATA(date)
             ).
      num8 = date.
      WRITE num8 TO bseg-zuonr USING EDIT MASK '____/__/__'.
    CATCH cx_abap_datfm_no_date cx_abap_datfm_invalid_date cx_abap_datfm_format_unknown cx_abap_datfm_ambiguous INTO DATA(oref).
      MESSAGE oref->if_message~get_text( ) TYPE 'E'.
  ENDTRY.  

Put the code in some FI validation rule exit for example.

Hello,

I have a requirement to validate date format of BSEG-ZUONR.

If the date format is not equal to YYYY/MM/DD, we need to print error message saying 'date format is incorrect'.

Can anyone let me know how this can be achieved? can we write a regex?

Thanks in advance.

Regards,

Tejas.

5 REPLIES 5
Read only

FredericGirod
Active Contributor
3,883
CONSTANTS c_regex_ymd_slash  TYPE string VALUE '(([12]\d{3})\/(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01]))'.
...
    FIND REGEX c_regex_ymd_slash IN iv_date_analysed.
    IF sy-subrc EQ space.
      "blabla
      RETURN.
    ENDIF.
....
Read only

abo
Active Contributor
0 Likes
3,883

Antoher idea would be to try casting it to a date a deal with exceptions but I have a soft spot for regex 😉

Read only

0 Likes
3,883

c5e08e0478aa4727abc4482f5be390b2 how can you be sure the date format is YYYY/MM/DD ?

Read only

abo
Active Contributor
0 Likes
3,883

Assign it to a date, write it back using formatting, compare with source. Hmm, come to think of it, the efficiency of this approach is highly questionable 😛

Read only

RaymondGiuseppi
Active Contributor
3,884

If you are not in the mood to use regex anymore, because you have realized that to insure actual dates, a regex is not the way to do it, you can try something like

  TRY.
      cl_abap_datfm=>conv_date_ext_to_int(
        EXPORTING
          im_datext    = bseg-zuonr
          im_datfmdes  = '5'
  IMPORTING
    ex_datint    = DATA(date)
             ).
      num8 = date.
      WRITE num8 TO bseg-zuonr USING EDIT MASK '____/__/__'.
    CATCH cx_abap_datfm_no_date cx_abap_datfm_invalid_date cx_abap_datfm_format_unknown cx_abap_datfm_ambiguous INTO DATA(oref).
      MESSAGE oref->if_message~get_text( ) TYPE 'E'.
  ENDTRY.  

Put the code in some FI validation rule exit for example.