DATA: lv_text TYPE string.
lv_text = 'AA_Test'.
* Prüfen, ob ein String mit AA oder BB oder CC beginnt
IF abap_true = cl_abap_matcher=>create( pattern = |^(AA\|BB\|CC).*$|
text = lv_text
ignore_case = abap_true )->match( ).
WRITE: / 'Match'.
ELSE.
WRITE: / 'No match'.
ENDIF.
Schlagwort: pattern
[ABAP] RegEx – String auf Float/Integer testen
* http://www.regular-expressions.info/floatingpoint.html
DATA: lv_in TYPE string VALUE '-1.23'. " oder 1.23E-10 oder -1
DATA: lv_out TYPE f.
DATA(matcher) = cl_abap_matcher=>create( pattern = '^[-+]?[0-9]*[.]?[0-9]+([eE][-+]?[0-9]+)?$'
text = lv_in
ignore_case = abap_true ).
IF matcher->match( ) = abap_true.
lv_out = lv_in.
WRITE: / lv_out.
ENDIF.
[ABAP] RegEx – String auf Datum testen
* http://www.regular-expressions.info/dates.html
DATA: lv_in TYPE string VALUE '2016-02-01'.
* mm-dd-yyyy, mm/dd/yyyy, mm.dd.yyyy, mm dd yyyy
* '^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20|21)\d\d$'
* dd-mm-yyyy, dd/mm/yyyy, dd.mm.yyyy, dd mm yyyy
* '^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$'
* yyyy-mm-dd, yyyy/mm/dd, yyyy.mm.dd, yyyy mm dd
* '^(19|20|21)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$'
DATA(matcher) = cl_abap_matcher=>create( pattern = '^(19|20|21)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$'
text = lv_in
ignore_case = abap_true ).
IF matcher->match( ) = abap_true.
WRITE: / lv_in.
ENDIF.
[ABAP] RegEx – String auf Leerzeichen (Spaces) testen
DATA: lv_txt TYPE string.
lv_txt = ...
IF abap_true = cl_abap_matcher=>create( pattern = '^ *$'
text = lv_text
ignore_case = abap_true )->match( ).
WRITE: / 'Der String besteht nur aus 0..n Leerzeichen. Er ist im Grunde leer.'.
ELSE.
WRITE: / 'Im String besteht nicht nur aus Leerzeichen.'.
ENDIF.
[ABAP] RegEx – String auf White-Spaces testen
DATA: lv_txt TYPE string.
lv_txt = ...
* White-Spaces:
* - space
* - tab (\t)
* - carriage-return (\r)
* - newline (\n)
* - form-feed (\f)
IF abap_true = cl_abap_matcher=>create( pattern = '^\s*$'
text = lv_txt
ignore_case = abap_true )->match( ).
WRITE: / 'Der String besteht nur aus 0..n White-Spaces. Er ist im Grunde leer.'.
ELSE.
WRITE: / 'Im String besteht nicht nur aus White-Spaces.'.
ENDIF.
[ABAP] RegEx – String auf gültiges Unix-Verzeichnis testen
IF abap_true = cl_abap_matcher=>create( pattern = '^((?:\/[a-zA-Z0-9]+(?:_[a-zA-Z0-9]+)*(?:\-[a-zA-Z0-9]+)*)+)$'
text = '/usr/sap/'
ignore_case = abap_true )->match( ).
WRITE: / 'Gültiges Unix-Verzeichnis.'.
ELSE.
WRITE: / 'Ungültige Unix-Verzeichnis.'.
ENDIF.
[ABAP] RegEx – String auf gültige Email-Adresse prüfen
Variante 1 (cl_abap_matcher)
* Übersicht TLD: https://data.iana.org/TLD/tlds-alpha-by-domain.txt
* Format Email-Adressen: https://de.wikipedia.org/wiki/E-Mail-Adresse
* einfaches Beispiel: ^[\w\.=-]+@[\w\.-]+\.[\w]{2,18}$
* komplexes Beispiel: ^[a-zA-Z0-9][A-Za-z0-9\.!#$%&'*+\-\/=?^_`{|}~]{0,28}[a-zA-Z0-9]@[äöü\w\.\-]+\.[\w]{2,18}$
DATA(lv_email) = |my.email@email.com|.
DATA(lv_regex) = |^[a-zA-Z0-9][A-Za-z0-9.!#$%&'*+-/=?^_`\{\|\}~]\{0,28\}[a-zA-Z0-9]@[äöü\\w.-]+.[\\w]\{2,18\}$|.
IF abap_true = cl_abap_matcher=>create_pcre( pattern = lv_regex
text = lv_email
ignore_case = abap_true )->match( ).
WRITE: / 'Gültige Email'.
ELSE.
WRITE: / 'Ungültige Email'.
ENDIF.
Variante 2 (xco_cp, xco_cp_regular_expression)
DATA(lv_email) = |my.email@email.com|.
DATA(lv_regex) = |^[a-zA-Z0-9][A-Za-z0-9.!#$%&'*+-/=?^_`\{\|\}~]\{0,28\}[a-zA-Z0-9]@[äöü\\w.-]+.[\\w]\{2,18\}$|.
* Regex nach PCRE (Perl Compatible Regular Expressions)
DATA(lv_match) = xco_cp=>string( lv_email )->matches( iv_regular_expression = lv_regex
io_engine = xco_cp_regular_expression=>engine->pcre( iv_ignore_case = abap_true
iv_enable_multiline = abap_true ) ).
IF abap_true = lv_match.
WRITE: / 'Gültige Email'.
ELSE.
WRITE: / 'Ungültige Email'.
ENDIF.