Regular Expression in ABAP

This article will teach you how to use Regular Expressions in ABAP code.

The command you need to use is REGEX. You can search Regular Expressions in a variable or a table.

FIND REGEX pattern
  IN TABLE table IGNORING CASE
  MATCH OFFSET moff
  MATCH LENGTH mlen
  SUBMATCHES s1 s2 ....

Suppose that you have the following text in a table, you read the text using READ_TEXT function and save the result in an internal table and now you need to interpret some values:

Part Lot.: YES
Telex Release.: YES
Sea Waybill.: NO
L/C(Cart. Crédito).: NO
DU.: YES- DU TEXTO
PIP.: YES - PIP TEXTO
MOZ.: NO
PIV.: YES - PIV TEXTO
Incoterms.: YES - INCOTERMS TEXTO
Quantidade CNTR.: YES - 123456
SKU Espelho.: YES
Inspeção.: YES
Etiqueta.: YES
Invoice.: Lorem ipsum dolor sit amet, consectetur adipiscing elit,..
Packing list.: Sed ut perspiciatis unde omnis iste natus error sit voluptatem...

You can use the following code to read values (YES/NO) at the end of the lines to determiine if the "DU" line have a content any content


  FIND REGEX '[Dd][Uu].*([Yy][Ee][Ss]|[Nn][Oo])\s*\-\s*(.*)'
    IN TABLE ti_texto IGNORING CASE
    MATCH OFFSET l_moff
    MATCH LENGTH l_mlen
    SUBMATCHES submatch1 submatch2.

The variable submatch1 tell us the DU is set to YES or NO while the submatch2 have the DU text "DU TEXTO".

Regex on ABAP is a powerful resource to validade contents.