[ABAP] RegEx: Submatches in einem String finden

Variante 1 (cl_abap_matcher)

* mehrere Submatches in einer vorgegebenen Ordnerstruktur finden
* Unterordner mit '/<Zahlen>'
DATA(matcher) = cl_abap_matcher=>create( pattern     = '/([0-9]{1,5})'
                                         text        = '/category/12345/item/12'
                                         ignore_case = abap_true ).

WHILE abap_true = matcher->find_next( ).
  WRITE: / matcher->get_submatch( 1 ).
ENDWHILE.

Variante 2 (cl_abap_matcher)

* mehrere Submatches in einer vorgegebenen Ordnerstruktur finden
* Ordnerstruktur mit '/category/<Zahlen>/item/<Zahlen>'
DATA(matcher) = cl_abap_matcher=>create( pattern     = '^/category/([0-9]{1,5})/item/([0-9]{1,2})$'
                                         text        = '/category/12345/item/12'
                                         ignore_case = abap_true ).

IF abap_true = matcher->match( ).
* erstes Match
  WRITE: / matcher->get_submatch( 1 ).
* zweites Match
  WRITE: / matcher->get_submatch( 2 ).
ENDIF.

Variante 3 (FIND ALL OCCURRENCES OF REGEX)

* Ordnerstruktur mit '/category/<Zahlen>/item/<Zahlen>'
DATA(lv_regex) = '^/category/([0-9]{1,5})/item/([0-9]{1,2})$'.
DATA(lv_text) = '/category/12345/item/12'.

* Alle Auftreten des Suchmusters
FIND ALL OCCURRENCES OF REGEX lv_regex IN lv_text RESULTS DATA(it_results).

* Ausgabe
LOOP AT it_results ASSIGNING FIELD-SYMBOL(<r>).
  LOOP AT <r>-submatches ASSIGNING FIELD-SYMBOL(<s>).
    WRITE: / substring( val = lv_text off = <s>-offset len = <s>-length ).
  ENDLOOP.
ENDLOOP.

Variante 4 (FIND REGEX)

DATA(lv_regex) = '^/category/([0-9]{1,5})/item/([0-9]{1,2})$'.
DATA(lv_text) = '/category/12345/item/12'.

* Erstes Auftreten des Suchmusters
FIND REGEX lv_regex IN lv_text RESULTS DATA(lv_results).

LOOP AT lv_results-submatches ASSIGNING FIELD-SYMBOL(<s>).
  WRITE: / substring( val = lv_text off = <s>-offset len = <s>-length ).
ENDLOOP.

[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 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.