Skip to Content

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • avatar image
    Former Member
    Mar 26, 2008 at 05:38 PM

    You're using "find_all". Should you be using "find_next"?

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Jerry,

      thank you for your reply, but I think if I will use

      gx_matcher->find_next( )
      

      I'll have to use it in combination with a loop.

      WHILE gx_matcher->find_next( ) = 'X'.
        TRY.
            CALL METHOD gx_matcher->get_match
              RECEIVING
                match = gs_match_result.
            APPEND gs_match_result TO gt_match_result2.
          CATCH cx_sy_no_current_match.
            BREAK-POINT.
            EXIT.
        ENDTRY.
      ENDWHILE.
      

      This delivers the same result as

      gx_matcher->find_all( ).
      

      Matthias