ABAP Regular Expression
ABAP Regular Expression
ABAPTechnicalSolutionsABAP<b>RegularExpressions</b>|SAPABAPTechnicalSolutions
chromeextension://iooicodkiihhpojmmeghjclgihfjdjhj/front/in_isolation/reformat.html
1/4
7/24/2015
ABAPTechnicalSolutionsABAP<b>RegularExpressions</b>|SAPABAPTechnicalSolutions
characters.
+
\<
\>
Used for denoting negation when used with box brackets, as well as the start of
line marker.
?=
?!
\1,\2 Used for placeholders for subgroup registers (also called the back-referencing
operator). For replacement, $1 and $2 represent the subgroup registers in the
replacement string (this will be discussed in a recipe ahead).
$
\d
\w
\u
All the three operators ( *, +, and ?) must be used after a character or a character
sequence specification. The box brackets denote the possible characters that may occur
in a string. On the other hand, the round brackets denote a specific set of characters in a
given sequence to be matched. For example, the regex [01]? will match 0 or 1. The
expression [01]* will match 01, 11 0101, and so on. On the other hand, (01)* will match
01, 0101, or blank. (Also, \w+ denotes one or more alphanumeric characters.)
The special characters that are used in regex may also need to be searched in a given text.
For searching them, we must precede them with a backslash (Escape Character for special
characters). Some of the examples are shown in the following table:
Searching for special characters
These are then treated as literals rather than operators.
The ?= is a preview condition. For example, if we write regex in the form a(?=s), the
expression behaves like an IF statement condition. The pattern a will be matched only if
the following substring matches the condition specified by s.
chromeextension://iooicodkiihhpojmmeghjclgihfjdjhj/front/in_isolation/reformat.html
2/4
7/24/2015
ABAPTechnicalSolutionsABAP<b>RegularExpressions</b>|SAPABAPTechnicalSolutions
There is a difference between ^ when used within box brackets [] and round brackets ().
When the ^ operator is used in box brackets, it represents the characters not included in
the text to be matched. For example, [^ab] will match all strings that do not include a and
b, such as cd, ch, hh, and so on. Whereas, ^ when used with round brackets (or without it)
specifies the beginning of a string. For example, ^(ab) or ^ab will match all strings
starting with ab, such as abc, abd, abbbbb, and so on.
For error handling, the CX_SY_REGEX class within the TRY and CATCH statements may
be used, if required.
For replace statement, when the replace has been done successfully, the return code SYSUBRC value is equal to 0. This may be used for checking the success of the replace
statement.
Example:
General Method
PARAMETERS: FLD1 TYPE C LENGTH 3 LOWER CASE.
IF FLD1 EQ 'ABC' OR
FLD1 EQ 'CDE' OR
FLD1 EQ 'DEF'.
WRITE: / 'Valid'.
ELSE.
WRITE: / 'Invalid'.
ENDIF.
[1]
3/4
7/24/2015
ABAPTechnicalSolutionsABAP<b>RegularExpressions</b>|SAPABAPTechnicalSolutions
Links
1. http://abaptechnicalsolutions.blogspot.in/2013/05/abap-regular-expressions.html
2. http://abaptechnicalsolutions.blogspot.in/2013/05/abap-regular-expressions.html
3. http://abaptechnicalsolutions.blogspot.in/2013/05/abap-regular-expressions.html
4. http://abaptechnicalsolutions.blogspot.in/2013/05/abap-regular-expressions.html
5. http://abaptechnicalsolutions.blogspot.in/2013/05/abap-regular-expressions.html
chromeextension://iooicodkiihhpojmmeghjclgihfjdjhj/front/in_isolation/reformat.html
4/4