0% found this document useful (0 votes)
112 views

ABAP Regular Expression

The document discusses ABAP regular expressions. It defines regular expressions as patterns that represent sets of strings. It describes operators like ., ?, *, +, \<, \>, ^, ?=, and ! that have special meanings in regular expressions. It provides examples of using regular expressions in ABAP with the FIND and REPLACE statements to search for patterns in strings and explains how to handle errors. It demonstrates how to use regular expressions to validate a field contains an accepted value more concisely than nested IF statements.

Uploaded by

nlpatel22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

ABAP Regular Expression

The document discusses ABAP regular expressions. It defines regular expressions as patterns that represent sets of strings. It describes operators like ., ?, *, +, \<, \>, ^, ?=, and ! that have special meanings in regular expressions. It provides examples of using regular expressions in ABAP with the FIND and REPLACE statements to search for patterns in strings and explains how to handle errors. It demonstrates how to use regular expressions to validate a field contains an accepted value more concisely than nested IF statements.

Uploaded by

nlpatel22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

7/24/2015

ABAPTechnicalSolutionsABAP<b>RegularExpressions</b>|SAPABAPTechnicalSolutions

SAP ABAP Technical Solutions


ABAP - Advanced Business Application Programming

ABAP Regular Expressions


ABAP Regular Expressions
Regular expressions can be used after the addition REGEX of the statements FIND and
REPLACE for searching in character strings.
The classes CL_ABAP_REGEX and CL_ABAP_MATCHER permit object-oriented use of
regular expressions.
A regular expression 'r' represents a set of character strings. If text is a character string
represented by 'r', we say that 'r' matches text or that 'r' fits text. Two (different) regular
expressions match if they fit the same set of character strings.
If you apply a regular expression to a character string text as a search string, then you are
searching for matches of the regular expression with substrings of text. In this case,
specific special characters of the regular expression do not match characters, but instead
match positions, thus influencing the type and number of found locations.
A regular expression comprises literals and operators. The operators are special
characters used for a particular purpose and have special meanings when we need to
search for any pattern within a text stream. Before the recipes, we will have a look at
some useful operators available in ABAP for regular expressions.
.

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

chromeextension://iooicodkiihhpojmmeghjclgihfjdjhj/front/in_isolation/reformat.html

1/4

7/24/2015

ABAPTechnicalSolutionsABAP<b>RegularExpressions</b>|SAPABAPTechnicalSolutions

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.

?=

Used as a preview condition.

?!

Used as a negated preview condition.

\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).
$

Denotes end of a line.

\d

Denotes a digit (0-9).

\w

Denotes an alphanumeric character.

\u

Matches a single alphabet.

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]

REGEX Method [2]


PARAMETERS: FLD1 TYPE C LENGTH 3.
FIND REGEX '[ABC|CDE|DEF]' IN FLD1.
IF SY-SUBRC EQ 0.
WRITE: / 'Valid'.
ELSE.
WRITE: / 'Invalid'.
ENDIF.
[3]

Here PIPE "|" symbol denotes "OR".


REGEX Method [4]
PARAMETERS: FLD1 TYPE C LENGTH 3.
chromeextension://iooicodkiihhpojmmeghjclgihfjdjhj/front/in_isolation/reformat.html

3/4

7/24/2015

ABAPTechnicalSolutionsABAP<b>RegularExpressions</b>|SAPABAPTechnicalSolutions

FIND REGEX '[ABC|CDE|DEF]' IN FLD1 IGNORE CASE.


IF SY-SUBRC EQ 0.
WRITE: / 'Valid'.
ELSE.
WRITE: / 'Invalid'.
ENDIF.
[5]

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

Get a free Evernote account to save this article and


view it later on any device.
Create account

chromeextension://iooicodkiihhpojmmeghjclgihfjdjhj/front/in_isolation/reformat.html

4/4

You might also like