Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Program Flow →  Expressions and Functions for Logical Expressions →  Logical Functions →  Predicate Functions →  Predicate functions for character-like arguments 

Predicate Function, matches

The example demonstrates the predicate function matches.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DO.
      CALL SELECTION-SCREEN 100.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      IF matches( val   = email
                  regex = `\w+(\.\w+)*@(\w+\.)+((\l|\u){2,4})` ).
        MESSAGE 'Format OK' TYPE 'S'.
      ELSEIF matches(
               val   = email
               regex = `[[:alnum:],!#\$%&'\*\+/=\?\^_``\{\|}~-]+`     &
                      `(\.[[:alnum:],!#\$%&'\*\+/=\?\^_``\{\|}~-]+)*` &
                     `@[[:alnum:]-]+(\.[[:alnum:]-]+)*`              &
                      `\.([[:alpha:]]{2,})` ).
        MESSAGE 'Syntax OK but unusual' TYPE 'S' DISPLAY LIKE 'W'.
      ELSE.
        MESSAGE 'Wrong Format' TYPE 'S' DISPLAY LIKE 'E'.
      ENDIF.
    ENDDO.

Description

The program checks the formal correctness of an e-mailaddress that has been entered by comparing it with regular expressions.

Whilst the first regular expression checks standard e-mail adresses without special characters, the second regular expression performs a more lenient syntax check according to RFC 822.

Even the second check, which uses a relatively simple regular expression for the example, does not always work for all e-mail addresses which were possible according to RFC 822.

The DEMO_VALIDATE_RFC_822_ADDRESS program uses a regular expression taken from the Internet, which should actually recognize all e-mail addresses allowed by RFC 822. The regular expression there was originally written for perl and no longer consists of more than 6000 characters. The program therefore serves as an example of how not to use regular expressions in ABAP.