Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Character String and Byte String Processing →  Expressions and Functions for String Processing →  Regular Expressions 

Regular Expressions

The example demonstrates how to search for regular expressions.

An extended example that also allows replacements is DEMO_REGEX_TOY.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    text = `Cathy's cat with the hat sat on Matt's mat.`.
    regx = '(.AT)|(\<.at\>)'.

    DO.

      TRY.

          result_wa = text.

          IF first = 'X' AND nocase = 'X'.
            REPLACE FIRST OCCURRENCE OF REGEX regx IN result_wa
                    WITH repl
                    IGNORING CASE.
          ELSEIF all = 'X' AND nocase = 'X'.
            REPLACE ALL OCCURRENCES OF REGEX regx IN result_wa
                    WITH repl
                    IGNORING CASE.
          ELSEIF first = 'X' AND case = 'X'.
            REPLACE FIRST OCCURRENCE OF REGEX regx IN result_wa
                    WITH repl
                    RESPECTING CASE.
          ELSEIF all = 'X' AND case = 'X'.
            REPLACE ALL OCCURRENCES OF REGEX regx IN result_wa
                    WITH repl
                    RESPECTING CASE.
          ENDIF.

        CATCH cx_sy_regex.

          MESSAGE 'Invalid Regular Expression' TYPE 'I'
                                              DISPLAY LIKE 'E'.
          CLEAR result_wa.

      ENDTRY.

      display( ).

    ENDDO.

Description

The example displays a screen in which a text line and a regular expression can be entered. The program scans the text line text for the first or all areas that match the search pattern defined in the regular expression regx and highlights these areas in a result field; you may select case distinction. For the search the statement REPLACE is used which embeds the found location between "@@tgl@@ to the left and "@@tgr@@ to the right. The operator $0 is used as a placeholder for the found location in the replacement text repl. In method display the text is formatted for the presentation of the found locations in HTML format and displayed.

The predefined example text is:

"Cathy's cat with the hat sat on Matt's mat."

and the predefined regular expression is:

(.AT)|(\<.at\>)

The regular expression describes

  • a sequence of three characters, where the first is any single character and the other two are "AT", or
  • a word made of three characters, where the first is any single character and the other two are "at".

Depending on the search settings, the following locations will be found:

  • The search for the first occurrence without case distinction finds the "Cat" of "Cathy". This location matches the expression .AT, but not the expression \<.at\>.
  • The search for the first occurrence with case distinction finds the word "cat". This location matches the expression \<.at\>, but not the expression .AT.
  • The search for all occurrences without case distinction finds all three subsequences consisting of three characters that end with "at": "Cat", "cat", "hat", "sat", "Mat", and "mat". All locations match the expression .AT. However, only the words "cat", "hat", "sat" and "mat" match the expression \<.at\>.
  • The search for all occurrences with case distinction finds the words "cat", "hat", "sat" and "mat". They all match the expression \<.at\>. But none of the locations matches .AT.