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

A built-in function or system classes can be used to test regular functions.

Other versions: 7.31 | 7.40 | 7.54

Using a Built-In Function

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 returns a truth value and can be used like a relational expression in the corresponding positions, for example after IF (see the executable example for matches). Case-sensitivity can be specified using the parameter case and the passed string can be restricted to a substring.


Note

The match function match, however, returns a regular expression that matches a regular expression.

Using System Classes

The system classes for regular expressions are CL_ABAP_REGEX and CL_ABAP_MATCHER.

  • The class CL_ABAP_REGEX creates an object-oriented representation from a regular expression in a character-like field.
  • The class CL_ABAP_MATCHER applies a regular expression created 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) = cl_abap_matcher=>create( pattern     = ...
                                         ignore_case = ...
                                         text        = ... ).

DATA(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) = NEW cl_abap_regex( pattern  =  ...
                                 ignore_case = ... ).

DATA(matcher) = regex->create_matcher( text = ... ).
                                       ignore_case = ... .
DATA(match) = matcher->match( ).
matcher = regex->create_matcher( text = ... ).