Regular Expressions (Regex) and its use in ABAP
Regular Expressions (Regex) is far from new, even in ABAP. ABAP supports regular expressions as of Release 7.00, released to customers on Oct 24, 2005. Although not so new its use is rarely found in ABAP. One reason is that SAP has already provided other ways to search for patterns e.g. SEARCH txt FOR pattern and IF txt CP pattern.
But what is Regex after all?
Wikipedia
“In computing, a regular expression also referred to as regex or regexp, provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. A regular expression is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.”
There is a very good tutorial about Regex at http://www.regular-expressions.info. We will skip the basics of Regex and go directly to its use in ABAP.
Using Regex in ABAP
Regex are extremely more powerful than traditional SAP patterns and is commonly used for searching and validating text. ABAP supports Regex in the statements FIND and REPLACE and via the the classes CL_ABAP_REGEX and CL_ABAP_MATCHER. There is an excellent article at SDN produced by Shaira Madhu with many more details of Regex in ABAP.
Let’s see some examples.
Example 1 – Tests if text starts with Hello (case sensitive)
DATA text TYPE string.
DATA moff TYPE i.
DATA mlen TYPE i.
text = `Hello World example`.
FIND REGEX ‘^Hello’ IN text MATCH OFFSET moff
MATCH LENGTH mlen.
IF sy-subrc = 0.
WRITE / text+moff(mlen).
ENDIF.
Example 2 – Removes example from the text only if it is the last word
DATA text TYPE string.
text = `Hello World example`.
REPLACE REGEX ‘example$’ IN text WITH ”.
IF sy-subrc = 0.
WRITE / text.
ENDIF.
Example 3 – Validates e-mail using the class CL_ABAP_MATCHER
DATA email TYPE string VALUE `webmaster@sapignite`. “missing .com
DATA matcher TYPE REF TO cl_abap_matcher.
matcher = cl_abap_matcher=>create(
pattern = `\w+(\.\w+)*@(\w+\.)+(\w{2,4})`
text = email ).
IF matcher->match( ) IS INITIAL.
MESSAGE ‘Invalid e-mail’ TYPE ‘I’ DISPLAY LIKE ‘E’.
ENDIF.
Tip: For testing Regex without coding you can use the ABAP program DEMO_REGEX_TOY.
Below are listed some links for those who want to start using Regex.
SDN
Regular Expression Processing in ABAP
http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/902ce392-dfce-2d10-4ba9-b4f777843182
Regex Toy- Testing Regular Expressions In ABAP
http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/1f9cd701-0b01-0010-87b8-f86f9b7b823f
ABAP Geek 14 – Regular Expressions Made Easy
http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/15768
Regular Expression in ABAP Book
http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/23476