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 

Testing Regular Expressions

Predefined functions or system classes can be used to test regular functions.

Other versions: 7.31 | 7.40 | 7.54

Using Predefined Functions

The predicate function matches can be used as follows to test whether a regular expression passed to regex matches the character string passed to val:

... matches( val = ... regex = ... ) ...

The function matches is handled like a relational expression and can be used as a logical expression, for example behind IF (see example for matches). The function is case-sensitive. If the test is not case-sensitive, the content must be converted accordingly before or during the function call. System classes can also be used.

Using System Classes

The system classes for regular expressions are CL_ABAP_REGEX and CL_ABAP_MATCHER.

  • The class CL_ABAP_REGEX generates an object-oriented representation from a regular expression in a character-like field.
  • The class CL_ABAP_MATCHER applies a regular expression generated using CL_ABAP_REGEX to either a character string or an internal table.

CL_ABAP_MATCHER is enough for simple tests of regular expressions:

DATA: matcher TYPE REF TO cl_abap_matcher,
      match   TYPE c LENGTH 1.

matcher = cl_abap_matcher=>create( pattern     = ...
                                   ignore_case = ...
                                   text        = ... ).

match = matcher->match( ).

The data object match contains the value "X" if the regular expression passed in pattern matches the character string passed in text.

The following program works in the same way, but creates an object of the class CL_ABAP_REGEX explicitly. This form has better performance than the short form above if the same regular expression is used multiple times for different texts.

DATA: regex TYPE REF TO cl_abap_regex,
      matcher TYPE REF TO cl_abap_matcher,
      match   TYPE c LENGTH 1.

CREATE OBJECT regex EXPORTING pattern     = ...
                              ignore_case = ... .

matcher = regex->create_matcher( text = ... ).

match = matcher->match( ).