Post

Using Regular Expressions in ABAP

Using Regular Expressions in ABAP

Regular expressions (regex) are a powerful tool used to search, match, and manipulate strings in programming. They are widely used for tasks like validation, parsing, and transformation of text data. In ABAP, regular expressions are supported natively for certain string operations, making it easier to work with complex text patterns.

🔍 Regular Expressions in ABAP

In ABAP, regular expressions can be utilized with the FIND and REPLACE statements. These operations allow you to search for patterns and manipulate strings based on those patterns.

🧑‍💻 Example of FIND:

The FIND statement can be used to search for occurrences of a regular expression in a string:

1
2
3
FIND ALL OCCURRENCES OF REGEX lv_regexp
  IN lv_field
  SUBMATCHES lv_sub1 lv_sub2.

Here:

  • lv_regexp is the regular expression pattern you’re searching for.
  • lv_field is the string in which you’re searching.
  • SUBMATCHES allow you to capture portions of the match, storing them in lv_sub1 and lv_sub2.

🔄 Example of REPLACE:

The REPLACE statement is used to replace occurrences of a regular expression in a string:

1
2
3
REPLACE ALL OCCURRENCES OF REGEX lv_regexp
  IN lv_input
  WITH lv_replacement.

In this case:

  • lv_regexp is the regular expression you’re looking to match.
  • lv_input is the string where the replacements will take place.
  • lv_replacement is the text that will replace the matched occurrences.

🆕 ABAP 6.40 and Later: CL_ABAP_REGEX and CL_ABAP_MATCHER

Starting from ABAP 6.40, SAP introduced the classes CL_ABAP_REGEX and CL_ABAP_MATCHER, which provide more advanced functionality for working with regular expressions. These classes offer an object-oriented approach to regex handling, similar to what you might see in Java, making regular expression handling in ABAP more powerful and flexible.

Here’s an example of how you can use these classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
DATA:  lv_value TYPE string VALUE '1234567890'.

DATA:  lo_regexp  TYPE REF TO cl_abap_regex,
       lo_matcher TYPE REF TO cl_abap_matcher.

CREATE OBJECT lo_regexp
  EXPORTING
    pattern     = '[A-Z]'   " Regex pattern to match uppercase letters
    ignore_case = abap_true. " Case-insensitive matching

lo_matcher = lo_regexp->create_matcher( text = lv_value ).

IF lo_matcher->match( ) EQ abap_true.
  WRITE:/ 'It's a match'.

ELSE.
  WRITE:/ 'Not a match'.

ENDIF.

✨ Key Points:

  • CL_ABAP_REGEX: This class represents a regular expression object. You define the pattern and any flags (like case sensitivity) here.
  • CL_ABAP_MATCHER: This class is used to perform matching operations. After creating a matcher object using the create_matcher method, you can check whether the string matches the regex pattern using the match method.

In this example, the regular expression "[A-Z]" is used to search for uppercase letters in the string '1234567890'. Since there are no uppercase letters, the output would be 'Not a match'.


🔑 Key Concepts of Regular Expressions

Here are a few core concepts and examples of regular expressions you can use in ABAP:

  1. Character classes:
    • [A-Za-z] – Matches any uppercase or lowercase letter.
    • \d – Matches any digit (equivalent to [0-9]).
    • \w – Matches any word character (letters, digits, and underscores).
    • \s – Matches any whitespace character (spaces, tabs, etc.).
  2. Quantifiers:
    • * – Matches 0 or more occurrences of the preceding element.
    • + – Matches 1 or more occurrences of the preceding element.
    • {n} – Matches exactly n occurrences of the preceding element.
    • {n,} – Matches n or more occurrences of the preceding element.
    • {n,m} – Matches between n and m occurrences of the preceding element.
  3. Anchors:
    • ^ – Anchors the match to the start of the string.
    • $ – Anchors the match to the end of the string.
  4. Grouping and Alternation:
    • () – Groups expressions together to apply quantifiers or to capture parts of the match.
    • | – Acts as an OR operator. For example, (abc|def) will match either abc or def.

đź§Ş Testing Regular Expressions in ABAP

Testing regular expressions in ABAP is simple and can be done using SAP’s built-in demo program DEMO_REGEX_TOY. This program provides an interactive way to test various regex patterns and see the results directly within the ABAP environment.

The demo program allows you to experiment with different regex patterns and see how they match against sample data. It’s a great tool for learning and testing regular expressions before implementing them in your own ABAP code.

DEMO_REGEX_TOY


đź›  Practical Use Cases for Regular Expressions in ABAP

  1. Validating Input: You can use regular expressions to validate user input, such as ensuring an email address or phone number matches a specific format.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    DATA: lv_email TYPE string VALUE 'test@example.com',
          lo_regexp TYPE REF TO cl_abap_regex,
          lo_matcher TYPE REF TO cl_abap_matcher.
    
    CREATE OBJECT lo_regexp
      EXPORTING
        pattern = '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$' " Email regex
        ignore_case = abap_true.
    
    lo_matcher = lo_regexp->create_matcher( text = lv_email ).
    
    IF lo_matcher->match( ) EQ abap_true.
      WRITE:/ 'Valid email address'.
    ELSE.
      WRITE:/ 'Invalid email address'.
    ENDIF.
    
  2. Extracting Information: You can use regular expressions to extract certain pieces of information from a string, such as extracting phone numbers from text or finding specific patterns in log files.

  3. Replacing Patterns: Regular expressions can be useful for cleaning up or transforming data, such as replacing all occurrences of a certain pattern (e.g., replacing all spaces with underscores or formatting phone numbers).


đź§  Final Thoughts

Regular expressions in ABAP are a powerful tool for string manipulation, validation, and extraction. With the introduction of classes like CL_ABAP_REGEX and CL_ABAP_MATCHER in ABAP 6.40, working with regular expressions in ABAP is now easier and more flexible than ever before. Whether you’re validating input, extracting data, or performing replacements, regular expressions can help you handle complex string operations with ease.

By leveraging built-in tools like the DEMO_REGEX_TOY program and understanding the core concepts of regular expressions, you can improve your ABAP coding skills and handle text-based tasks more efficiently.

This post is licensed under CC BY 4.0 by the author.