2016 Jun 27 5:22 PM
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
2016 Jun 29 4:29 PM
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.
2016 Jun 28 8:06 AM
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.' ).
2016 Jun 29 3:42 PM
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
2016 Jun 29 4:37 PM
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 ).
2016 Jun 29 4:59 PM
Where .* is the most simple but sometimes not the best way, because it matches everything and is greedy etc. I normally prefer [^ ... ]* expressions.
2016 Jun 29 4:29 PM
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.
2016 Jun 29 5:38 PM
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