Use Subgroups in Regular Expressions to Solve Problems Quickly and Easily

  • by Rehan Zaidi, Senior SAP Technical Consultant
  • August 3, 2015
Rehan Zaidi explains how the use of subgroups within a Regular expression (Regex) allows you to write short code for solving complex programming problems. See six examples of how such regular expressions in conjunction with a REPLACE statement can save time and coding effort.
Learning Objectives

Reading this article, you will learn:

  • The basics of a Regular expression (Regex)
  • Use of subgroups with REPLACE statements
  • Date conversion into an SAP internal format
  • Removal of repeated words from a sentence
  • Removal of comments from an ABAP program
  • Validation and conversion of telephone numbers
  • Placement of commas in the Amount field
  • Removal of unwanted spaces from a text string
Key Concept

A Regular expression (Regex) is composed of literals and operators, both of which are special characters used to search for a certain pattern within a text stream. Specifying subgroups within a Regex lets you match one or more subsets of a given text. The matched content, stored in registers and used in conjunction with a REPLACE statement, can be used to fulfill data conversion requirements.

From SAP NetWeaver release 7.0, ABAP allows use of POSIX-style Regular expressions (Regexes) in your programs. Regexes let you write code composed of a few lines that would otherwise make the program too verbose if an alternate approach were used. Via Regexes, you can specify subgroups. If used wisely, subgroups can help write validations and conversion code very quickly and easily.

The purpose of this article is to provide a detailed explanation of subgroup use in Regex programming in conjunction with other operators and the REPLACE statement. I start with a brief explanation of the important elements and then explain what subgroups and subgroup registers are and how they fit into the landscape of Regex programming. Finally, I show six problems that can be solved using Regexes and the REPLACE statement.

I have selected examples that help you better understand the concept of Regex subgroups. The examples also help solve issues faced in real-life scenarios in development work. While a detailed explanation of Regex programming is beyond the scope of this article, I do discuss the basic operators needed to understand the subgroup-related examples and use the standard program DEMO_REGEX_TOY for illustration where required.

Rehan Zaidi

Rehan Zaidi is a consultant for several international SAP clients (both on-site and remotely) on a wide range of SAP technical and functional requirements, and also provides writing and documentation services for their SAP- and ABAP-related products. He started working with SAP in 1999 and writing about his experiences in 2001. Rehan has written several articles for both SAP Professional Journal and HR Expert, and also has a number of popular SAP- and ABAP-related books to his credit.

See more by this author


Comments

No comments have been submitted on this article. 


Please log in to post a comment.

To learn more about subscription access to premium content, click here.