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
You can use predefined functions or system classes to test regular functions.
Other versions: 7.31 | 7.40 | 7.54
Using Predefined Functions
You can use the predicate function matches
as follows to test whether a regular expression passed to regex
matches the character string passed to val
:
... matches( val = ... regex = ... ) ...
The function matches
can be used like a logical expression, for example behind
IF
(see example
for contains
). The function is case-sensitive. If you do not want your
test to be case-sensitive, the content must be converted accordingly before or during the function call. You can also use system classes.
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:
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.
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( ).