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 'S'
                                              DISPLAY LIKE 'E'.
          CLEAR result_wa.

        CATCH cx_sy_regex_too_complex.

          MESSAGE 'Regular Expression too Complex' TYPE 'S'
                                                  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. The search can be case-sensitive, if required. The search uses the statement REPLACE, which embeds the occurrence between "@@tgl@@ on the left and "@@tgr@@ on the right. The operator $0 is used as a placeholder for the occurrence in the replacement text repl. The method display formats the text for the representation of the occurrences in HTML format and displays it.

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 occurrences are found:

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