[ABAP] RegEx – Postleitzahl (PLZ) und Ort aus String filtern

* https://regex101.com/

* 12345 Berlin
* D12345 Berlin
* D-12345 Berlin
* D 12345 Berlin
* d12345 Berlin
* d-12345 Berlin
* d 12345 Berlin
* D-12345 Berlin-Tegel
* D-12345 Berlin (Bezirk Tegel)

DATA: lv_in TYPE string VALUE 'D-12345 Berlin (Bezirk Tegel)'.

DATA(matcher) = cl_abap_matcher=>create( pattern = '^(?:[Dd][- ]?)?\d{5}\s'
                                         text = lv_in
                                         ignore_case = abap_true ).

DATA(it_matches) = matcher->find_all( ).

IF NOT it_matches IS INITIAL.
* der erste Eintrag sollte die PLZ sein
  DATA(lv_plz) = it_matches[ 1 ].

* PLZ
  WRITE: / substring( val = lv_in
                      off = lv_plz-offset
                      len = lv_plz-length - 1 ).

* Ort
  WRITE: / substring( val = lv_in
                      off = lv_plz-length
                      len = strlen( lv_in ) - lv_plz-length ).
ENDIF.