Application Development and Automation 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: 
Read only

Regular Expression: Different results when using FIND or regex classes

mamHB
Explorer
0 Likes
772

Hi,

has anybody an idea why the FIND REGEX statement and the regex class delivers different results when searching for


^\*

in


****

I try to find every place with an asterisk as first sign of the line.

The FIND statement


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

returns one hit as expected. But the class cl_abap_regex and cl_abap_matcher returns four hits in this example:


DATA gv_string        TYPE        string.
DATA gv_pattern       TYPE        string.
DATA gt_match_result2 TYPE        match_result_tab.
DATA gx_regex         TYPE REF TO cl_abap_regex.
DATA 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 .
    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.

Looks like the class doesn't consider the start of line symbol (^). Is there an error in my implementation?

Any help is appreciated.

Matthias

2 REPLIES 2
Read only

Former Member
0 Likes
520

Hi!

Not sure, but the documentation of hat (^) in ABAP's Find command says "Negation of a value set for single characters" (FIND->FIND - pattern ->Syntax of Regular Expressions ->Special Characters in Regular Expressions).

Best!

Jim

Read only

0 Likes
520

Hi Jim,

thank you for your reply. But to negate a value set of single characters you have to use


[^  ]

I used just the character


^

This is defined as anchor character for the start of a line (See the documentation link you posted -> chapter 'Special characters for search strings') .

As I understand it, should the FIND REGEX statement does the same as the CL_ABAP_REGEX and CL_ABAP_MATCHER classes. Therefore, I do not understand, why both implementations deliver another result.

And I didn't find an answer for that. Does anyone else?

Matthias