Saturday, January 5, 2013

Regular Expressions (Regex) and its use in ABAP

Supported as of Release 7.00 Regular Expressions (Regex) are rarely found in ABAP. Have you ever heard something about Regex? We believe so. This article will give you a brief overview and present very good articles from SDN.


Even in ABAP Regular Expressions (Regex) is not a new issue. 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 of reasons 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 is 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.
DEMO_REGEX_TOY screenshot
DEMO_REGEX_TOY screenshot
ABAP Geek 14 – Regular Expressions Made Easy
http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/15768

No comments: