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: 

cl_abap_matcher and [[:unicode:]]

0 Kudos
1,236

Hello experts,

I'm having difficulty using ABAP class cl_abap_matcher with special characters.  Specifically, I'm trying to find any unicode characters in a string using cl_abap_matcher with the following code:

p_instring = 'THIS IS A TEST.  你好 THIS IS ONLY A TEST.'

matcher = cl_abap_matcher=>create( pattern = '[[:unicode:]]'

                                                        text     = p_instring ).

if matcher->match( ).

"match found

...

However, if I use the "find regex" version:


p_instring = 'THIS IS A TEST.  你好 THIS IS ONLY A TEST.'

find regex '[[:unicode:]]' in p_instring

this works fine.

Thanks in advance for any help!

Bruce

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
428

Hey, isn't that simply because a matcher matches the whole string while FIND looks for a portion in a string??

Without putting something appropriate around the current regex, of course it won't work. Use DEMO_REGEX for trying.

B.T.W. there are also built-in functions MATCH and MATCHES.

ABAP Keyword Documentation

6 REPLIES 6

Sandra_Rossi
Active Contributor
0 Kudos
428

It's because matcher->match( ) does not return a boolean but a structure of type MATCH_RESULT !

Instead, you may use if cl_abap_matcher=>matches( pattern = '[[:Unicode:]]' text = 'THIS IS A TEST ... ONLY A TEST.' ).

0 Kudos
428

Hi Sandra,

Thank you for the response but matcher->match( ) returns a type ABAP_BOOL value from local variable "SUCCESS":

For a test, I also tried to use cl_abap_matcher=>matches... but got the same results - false for [[:unicode:]] test:

lv_match = cl_abap_matcher=>matches( pattern = <fs_str_rpl>-srch_regex

                                                             text     = 'THIS IS A TEST.  你好 THIS IS ONLY A TEST.' ).

Thanks again for the reply!

Bruce

0 Kudos
428

Sorry, I made a confusion with matcher->get_match( ).

Horst is right! It works with: if cl_abap_matcher=>matches( pattern = '.*[[:unicode:]].*' text = p_instring ).

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
428

Where .* is the most simple but sometimes not the best way, because it matches everything and is greedy etc. I normally prefer [^ ... ]* expressions.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
429

Hey, isn't that simply because a matcher matches the whole string while FIND looks for a portion in a string??

Without putting something appropriate around the current regex, of course it won't work. Use DEMO_REGEX for trying.

B.T.W. there are also built-in functions MATCH and MATCHES.

ABAP Keyword Documentation

0 Kudos
428

Thank you both for the feedback!  My requirement is to handle both a find and replace scenario using cl_abap_matcher given the same regex input.  Since the replace feature works with [[:unicode:]] just fine, I didn't want to use a different search string when using match or matches.

I didn't realize that those to methods used the entire string.  Now, rather than using match or matches, I'm simply using find_next and the same regex expression can work for both find and replace search types.

...

lc_matcher = cl_abap_matcher=>create( pattern = '[[:unicode:]]'

                                                            text     = 'THIS IS A TEST.  你好 THIS IS ONLY A TEST.' ).

if lc_matcher->find_next( ) = abap_true.

...

This works perfectly.  Thanks again for the feedback!  Wouldn't have figured it out without the help!

Bruce