Sunday, 7 August 2016

ABAP Regular Expressions Usage


Filter strings of pattern LNN_NNNN_NN_LL (L – Letter, N – Number) from a list Eg: Z00_0002_01_AA, Z01_0015_02_AA etc.


Used SAP regular expressions (REGEX) concept to get this done in a program. It makes string, integer and special character comparisons much simpler.
'\<Z\d\d_\d\d\d\d_\d\d_\D\D\>' is the matching pattern in terms of regular expressions for a string likeZ00_0002_01_AA’.

DATA: c_pattern TYPE string VALUE '\<Z\d\d_\d\d\d\d_\d\d_\D\D\>'.
DATA:lv_match   TYPE abap_bool,
TYPE REF TO cl_abap_matcher.
lr_matcher cl_abap_matcher=>create
pattern     c_pattern
text        = ‘Z00_0002_01_AA’ ).
lv_match lr_matcher->match( ).

If lv_match = ‘X’ then it is a match.

Special characters used
Placeholder for any single digit
Placeholder for any character other than a digit
Start of a word
End of a word

There is a standard program available to check REGEX Functionality DEMO_REGEX_TOY. Below are few screens from the same.

Finding the pattern:

No comments:

Post a Comment