[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: Bestimmte Nodes (Submatches) in einem XML-String finden

* XML
DATA(lv_xml) = |<person>| &&
               |  <name>Udo</name>| &&
               |  <age>25</age>| &&
               |</person>| &&
               |<person>| &&
               |  <name>Ede</name>| &&
               |  <age>34</age>| &&
               |</person>| &&
               |<person>| &&
               |  <name />| &&
               |  <age>78</age>| &&
               |</person>|.

* Alle Nodes mit <name>...</name> finden
DATA(matcher) = cl_abap_matcher=>create( pattern     = '<name>([[:alnum:]]*)</name>'
                                         text        = lv_xml
                                         ignore_case = abap_true ).

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