SAP4TECH

SAP and ABAP Free Tutorials
Menu
  • SAP Technical
    • ABAP Code Snippets
    • ABAP WebDynPro
    • SAP GW
    • SAP IDOC (ALE)
    • SAP PI (XI)
    • SAP Screen Personas
    • SAP Workflow
  • SAP Functional
    • SAP FI
    • SAP FSCM
    • SAP HR
    • SAP SD & SAP MM
    • SAP PM
    • SAP PP
    • SAP PS
    • SAP QM
    • SAP VIM
    • SAP WM
  • SAP Fiori
  • SAP HANA
  • SAP BW
  • SAP CRM
  • SAP SRM
  • SAP4TECH
    • About Us
    • Contact Us
    • Terms & Conditions
    • Privacy and Cookie Policy

SAP4TECH » Using Regular Expressions in SAP ABAP (REPLACE, FIND REGEX)

ABAP Tutorials

Using Regular Expressions in SAP ABAP (REPLACE, FIND REGEX)

July 24, 2017 John

Start using Regular Expressions with ABAP and your Code will be more efficient and shorter. FIND REGEX and REPLACE ABAP statement with samples and real case.

In this article, we will start with the common Regular Expressions Operators used in ABAP.

Then, you will find some useful ABAP Statement using Regular Expressions in order to detail the main Regex in ABAP: the FIND REGEX and REPLACE. 

Table of Contents

  • List of Regular Expressions Operators (in ABAP)
  • Useful ABAP Statement using Regular Expressions
    • REPLACE OF REGEX
    • FIND REGEX statement in ABAP

List of Regular Expressions Operators (in ABAP)

Let’s start first by the list of Regular Expressions’ Operator

Operator Purpose
. Dot matches a single character.
? Denotes either no or a single occurrence of a character or set of characters.
* Denotes any number of occurrences (0/1/ or more) of a character or a set of characters.
+ Matches one or more occurrence of a character or set of characters.
\< Matches start of a word. \> ?Matches end of a word.
^ Used for denoting negation when used with box brackets as well as the start of line marker.
?= as a preview condition.
?! Used as a negated preview condition.
\1 placeholders for subgroup registers .
\2 Used for placeholders for subgroup registers.
$ Denotes end of a line.
\d Denotes a digit (0-9).
\w Denotes an alphanumeric character
\u Matches a single alphabet

Useful ABAP Statement using Regular Expressions

You will find some useful usage of Regular Expressions in ABAP

Check also Two ways to Check if email is valid in ABAP : Function Vs Regex

REPLACE OF REGEX

Syntax of REPLACE pattern IN  

The Syntax of REPLACE  pattern IN ABAP programming is the following

REPLACE [{FIRST OCCURRENCE}| {ALL OCCURRENCES} OF] pattern
        IN [section_of] dobj WITH new
        [IN {CHARACTER|BYTE} MODE]
        [replace_options].

Look at this example of how to use REPLACE with Regular Expressions.
This ABAP Snippets will replace all the u in the text string with x

ABAP
1
2
3
4
" After being replaced, text contains the value "x-xx-x".
DATA text TYPE string VALUE '-uu-'.
 
REPLACE ALL OCCURRENCES OF REGEX 'u*' IN text WITH 'x'.

Exceptions to REPLACE OF REGEX

In order to handle ABAP Regular Expression Errors, SAP added 3 Regex Exceptions.

Here the list of Exceptions to REPLACE OF REGEX

  • CX_SY_INVALID_REGEX
    • Cause: Illegal expression after the addition REGEX.
      Runtime Error: INVALID_REGEX
  • CX_SY_REGEX_TOO_COMPLEX
    • Cause: More information: Exceptions in Regular Expressions.
      Runtime Error: REGEX_TOO_COMPLEX
  • CX_SY_INVALID_REGEX_FORMAT
    • Cause: Invalid replacement pattern after the addition WITH.
      Runtime Error: INVALID_FORMAT

Removing Extra characters in Phone Number using FIND REGEX in ABAP

Here another example, and useful one. In order to remove none numerical from a phone number, you use the following line of ABAP Code :

1
2
REPLACE ALL OCCURRENCES OF REGEX '([^\d])'
   IN LV_PHONE WITH ''.

FIND REGEX statement in ABAP

In this variant, a search is performed for a match with a regular expression specified in regex.

Character-like expression position

For regex, either a character-like operand can be specified that contains a valid, regular expression when the statement is executed, or an object reference variable that points to an instance of the class CL_ABAP_REGEX.

If specified directly, regex is a character-like expression position.

In searches for a regular expression, specific search strings can be entered that permit further conditions including forecast conditions.

The occurrences are determined according to the “leftmost-longest” rule.

ABAP Regex: Multiple Matches

Of all the possible matches between the regular expression and the required character string, the substring starting in the furthest position to the left is selected.

If there are multiple matches in this position, the longest of this substring is selected.

An empty substring in regex is not a valid regular expression and raises an exception. A character string is empty if regex is either an empty string or is of type c, n, d, or t and only contains blanks.

Some regular expressions that are not empty, such as a*, are used to search for empty character strings. This is possible when searching for the first occurrence or all occurrences.

The relevant empty substring are found before the first character, between all characters, and after the last character of the search ranges. A search of this type is always successful.

A regular expression can have correct syntax, but be too complex for the execution of the statement FIND, which raises a handleable exception to the class CX_SY_REGEX_TOO_COMPLEX. Refer to Exceptions in Regular Expressions.

Example of FIND REGEX

The following search finds the substring ‘ababb’ from offset 3 or higher.

Using the “leftmost-longest” rule, the other matching substring ‘babboo’ from offset 4 or higher is not found.

1
2
3
4
5
6
DATA: moff TYPE i,
      mlen TYPE i.
 
FIND REGEX 'a.|[ab]+|b.*' IN 'oooababboo'
     MATCH OFFSET moff
     MATCH LENGTH mlen.

An other example can be ( replace multiple if )

1
2
3
4
5
6
FIND REGEX '[0-9]' IN LV_DATA.
IF SY-SUBRC = 0.
WRITE: 'Numeric Found'.
ELSE.
WRITE: 'No Numeric Found'.
ENDID.

Note Also that you can use the FIND REGEX with ‘ignoring case’ if the uppercase makes no difference with lowercase in your case.

Or even you can concatenate condition with OR (|) in the Regular Expressions.

Prev Article
Next Article
Tags:FIND REGEX Regular Expressions

Related Articles

Function Module SAP Tables e1457710358957
Function Module SAP Tables storing information about SAP function modules. …

Function Module SAP Tables and Interfaces

chrysler invoice 46370 o e1442487543248
Idoc INVOIC02: This article will introduce you to outbound IDoc …

SAP Invoice IDoc INVOIC02 – Structure, User-Exit and BADI

Search on SAP4TECH

The Most Populars

  • The Most Important SAP ISU Tables
    The Most Important SAP ISU Tables
  • SAP Users Tables
    SAP Users Tables (for Personal, Logon, and Address Data)
  • List of SAP Purchase Order Tables in SAP MM SAP PO Tables
    SAP Purchase Order Tables: Main PO tables in SAP MM – SAP PO Tables
  • Full list of SAP Movement Types
    SAP Good Movement Types – Full list of SAP Movement Types
  • The Main SAP GL Account Tcodes SAP GL Account Tables
    The Main SAP G/L Account Tcodes & SAP GL Account Tables
  • SAP BOM Tables
    SAP BOM Tables for BOM Header, Items and components and Category

Related Posts

  • SAP Production Order Tables e1459012874669
    SAP Production Order Tables and Join Between PO Tables (AUFK, AFVC, AFFL, AFRU…)
  • Calculate Due Date1 e1452811957210
    Calculate Due Date in ABAP (BAPI DETERMINE_DUE_DATE)
  • SAP File
    How to zip a file using ABAP in SAP with CL_ABAP_ZIP
  • SAP ABAP
    How to SAP Upload Excel File with TEXT_CONVERT_XLS_TO_SAP
  • SAP FSCM
    Credit Limit for Customer in SAP SD

SAP4TECH

SAP and ABAP Free Tutorials

Trending Posts

  • The Most Important SAP ISU Tables
  • SAP Users Tables (for Personal, Logon, and Address Data)
  • SAP Purchase Order Tables: Main PO tables in SAP MM – SAP PO Tables
  • SAP Good Movement Types – Full list of SAP Movement Types
  • The Main SAP G/L Account Tcodes & SAP GL Account Tables
  • SAP BOM Tables for BOM Header, Items and components and Category

The Most Recents

  • The Most Important SAP ISU Tables
  • SAP Fiori 3 UX and Design of SAP Fiori Apps for SAP S/4HANA, SAP TechEd Lecture
  • The Main SAP Dunning Transaction Codes
  • SAP Accounts Payable Tcodes & Accounts Receivable Tcodes ( SAP AP Tcodes & SAP AR Tcodes)
  • The Most Important SAP Payment Terms Tables (ZTERM, Text…)

Search

SAP Tutorials by Topics

  • SAP Tables
  • SAP Tcodes
  • SAP BAPI
  • ABAP Snippets
  • Top SAP Courses
  • Top SAP Books
Copyright © 2021 SAP4TECH

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
This website uses cookies to improve your experience. We'll assume you accept this policy as long as you are using this websiteAcceptView Policy