Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Regex: Different results when using FIND or regex classes

Hi,

I am just playing around with Regular Expressions and I found something I could not explain. I try to find the regex

^\*

in string

****

.

The FIND statement delivers the expected result (just one hit; the first asterisk in string), but the matcher class finds all asterisks.

Has anybody an idea? Do I use the classes in a wrong way? Or is there already a known issue I didn't find yet?

DATA:
  gv_string        TYPE        string,
  gv_pattern       TYPE        string,
  gt_match_result  TYPE        match_result_tab,
  gt_match_result2 TYPE        match_result_tab,
  gx_regex         TYPE REF TO cl_abap_regex,
  gx_matcher       TYPE REF TO cl_abap_matcher.

gv_string = '****'.
gv_pattern = '^\*'.
FIND ALL OCCURRENCES OF REGEX gv_pattern
                           IN gv_string
                      RESULTS gt_match_result.

TRY.
    CREATE OBJECT gx_regex
      EXPORTING
        pattern = gv_pattern.
  CATCH cx_sy_regex .
    BREAK-POINT.
    EXIT.
ENDTRY.

TRY.
    CREATE OBJECT gx_matcher
      EXPORTING
        regex = gx_regex
        text  = gv_string.
  CATCH cx_sy_matcher .
    BREAK-POINT.
    EXIT.
ENDTRY.
gt_match_result2 = gx_matcher->find_all( ).
BREAK-POINT.

The DEMO_REGEX_TOY is not a great help, cause it is just using the FIND statement.

Every help is appreciated.

Matthias

Tags:
Not what you were looking for? View more on this topic or Ask a question