Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Regex: Different results when using FIND or regex classes

mamHB
Explorer
0 Kudos

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

3 REPLIES 3

Former Member
0 Kudos

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

0 Kudos

Try this:

DATA:
  gv_string        TYPE        string,
  gv_pattern       TYPE        string,
  gv_bool          type c,
  gs_match_result  TYPE        match_result,
  gx_regex         TYPE REF TO cl_abap_regex,
  gx_matcher       TYPE REF TO cl_abap_matcher.


gv_string = '****'.
gv_pattern = '^\*'.

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

TRY.
    CREATE OBJECT gx_matcher
      EXPORTING
        regex = gx_regex
        text  = gv_string.
  CATCH cx_sy_matcher .
    EXIT.
ENDTRY.


CALL METHOD gx_matcher->find_next
  RECEIVING
    success = gv_bool.


TRY.
    CALL METHOD gx_matcher->get_match
      RECEIVING
        match = gs_match_result.
  CATCH cx_sy_no_current_match.
ENDTRY.

0 Kudos

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