2013 May 21 1:23 PM
Hi Guys,
I have a question. I would like to see if two strings matches. It should be case sensitive. One can contain wildcard * or +.
I have found the pradicate matches(val = , ...) unfortunately i cannot use it because my system is old. I have also tried with CP but it is unfortunately not case sensitive.
Please help me.
Ex. String A should match string B.
1. A = Partnership , B = Partnership => match
2. A = 'PartnershiP' , B = Partnership => no match
3. A = *artne* , B = Partnership => match
4. A = 'PartNership' , B = 'Partnership' => no match
Can I use FIND regex in ... or FIND all Occurences?
I dont know how to express regex because the * or + can appear at any place in the string.
Please help me to find the best solution. Thanks.
Thanks.
2013 May 22 8:09 AM
I like regex, so here is a working snippet for you using regex.
For wildcard * , regex equivalent is .*
REPLACE statement is used to replace * with regex wildcard.
In case your search string has real asterisk instead of wildcard, you need to replace * with \*
*1. A = Partnership , B = Partnership => match
PERFORM matching USING 'Partnership' 'Partnership'.
*2. A = 'PartnershiP' , B = Partnership => no match
PERFORM matching USING 'PartnershiP' 'Partnership'.
*3. A = *artne* , B = Partnership => match
PERFORM matching USING '*artne*' 'Partnership'.
*4. A = 'PartNership' , B = 'Partnership' => no match
PERFORM matching USING 'PartNership' 'Partnership'.
*&---------------------------------------------------------------------*
*& Form matching
*&---------------------------------------------------------------------*
FORM matching USING a TYPE string
b TYPE string.
DATA lv_result TYPE string.
REPLACE ALL OCCURRENCES OF '*' IN a WITH '.*'.
FIND FIRST OCCURRENCE OF REGEX a IN b.
IF sy-subrc EQ 0.
lv_result = 'match'.
ELSE.
lv_result = 'no match'.
ENDIF.
WRITE:/(30) a,
(30) b,
lv_result.
ENDFORM. "matching
2013 May 21 1:35 PM
Hi Maria,
TRANSLATE string A TO UPPER CASE.
TRANSLATE string B TO UPPER CASE.
Now compare both.
Regards,
Mordhwaj
2013 May 21 1:43 PM
2013 May 21 1:37 PM
Hi,
Hope this will clear your doubts on how to use these variable .For case sensitive search
you can use #
Character | Used to |
* | Match any sequence of characters. |
+ | Match any single character. |
# | Interpret the next character literally. |
Table Using Characters
Statement | True When |
v1 CP 'A+C' | v1 contains "a" in the first position and "c" in the third. Either character can be in upper- or lowercase. Any character can appear in the second position. |
v1 CP '*Ab*' | The string "ab" occurs anywhere within v1. Either character can be in upper- or in lowercase. |
v1 CP '*#A#b*' | v1 contains a capital A followed by lowercase b. |
2013 May 21 1:45 PM
If I understand. I need to write a method that add # in front of all the character in the searchstring.
2013 May 21 1:53 PM
Hello Maria,
Use the following link.
https://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3516358411d1829f0000e829fbfe/content.htm
Hope it would be helpful...
Regards,
Navneet
2013 May 21 2:38 PM
Thanks but I have already tried CP (Contains Pattern). It didnt work because it was not case sensitive.
2013 May 21 2:04 PM
Hi Maria,
Try this example:
DATA:
lv_string1 TYPE string VALUE 'Satish',
lv_string2 TYPE string VALUE 'SatisH'.
FIND REGEX lv_string1 IN lv_string2.
IF sy-subrc = 0.
WRITE /'Matches'.
ELSE.
WRITE /'Do not matches'.
ENDIF.
2013 May 21 2:14 PM
lv_string1 should contain * or + and the search should be case sensitive.
I have tried it. It doesnt work.
2013 May 21 3:02 PM
Hi Maria,
what's the problem? Regex is case sensistive if not otherwise specified.
FIND REGEX 'PartnershiP' IN 'Partnership' . "SY-SUBRC = 4, no match
FIND REGEX 'Partnership' IN 'Partnership' . "SY-SUBRC = 0, match
FIND REGEX 'artner' IN 'Partnership' . "SY-SUBRC = 0, match
Regards
Clemens
2013 May 21 3:15 PM
You are right. My question is: how do I express the REGEX with + or * in it. It doesnt work. The search string is not always fully given. I may have 'Partner*' or '*Partne*ip' .
Thanks
2013 May 21 3:18 PM
I think you need to be more specific.
especially when you state: " * or + can appear at any place in the string."
That more or less means everything is possible. Basically you are saying Find regex * in anystring. Which will Always find a match.
You will have to state what are you looking for within a string. Which part is static and which part you want to use wildcards for.
A simple (real) example can clarify a lot.
Furthermore check the program DEMO_REGEX_TOY in your SAP system, you can try around with this program.
And a last remark is that you are referring to wild card +, but I don't think you can use that in regular expressions. Wildcxards are . (dot) for a single character and * for zero or more of the SAME characters.
FIND regex '.artne.' in 'Partner' RESPECTING CASE. would give a match
while
FIND regex '.artne.' in 'ParTner' RESPECTING CASE. would not return a match.
OR
FIND regex '.ar.*' in 'Partner' RESPECTING CASE. would give a match
while
FIND regex '.art.*' in 'ParTner' RESPECTING CASE. would not return a match.
2013 May 22 5:33 AM
Hi,
Try this code:
DATA: matcher TYPE REF TO cl_abap_matcher,
match TYPE c LENGTH 1.
matcher = cl_abap_matcher=>create( pattern = 'S(.*)ish'
ignore_case = ''
text = 'Satish' ).
match = matcher->match( ).
IF match = 'X'.
WRITE /'Matches'.
ELSE.
WRITE /'Do not matches'.
ENDIF.
2013 May 21 4:55 PM
Hi,
There are standard FMs available to convert case into one another. Better use them, as they work for different languages too.
Search for such FM using keywords, Translate, Upper etc., in SE37.
One such FM is TERM_TRANSLATE_TO_UPPER_CASE. If you dont find this, search for anyother rather than using TRANSLATE statement.
Regards
Karthik
2013 May 22 8:09 AM
I like regex, so here is a working snippet for you using regex.
For wildcard * , regex equivalent is .*
REPLACE statement is used to replace * with regex wildcard.
In case your search string has real asterisk instead of wildcard, you need to replace * with \*
*1. A = Partnership , B = Partnership => match
PERFORM matching USING 'Partnership' 'Partnership'.
*2. A = 'PartnershiP' , B = Partnership => no match
PERFORM matching USING 'PartnershiP' 'Partnership'.
*3. A = *artne* , B = Partnership => match
PERFORM matching USING '*artne*' 'Partnership'.
*4. A = 'PartNership' , B = 'Partnership' => no match
PERFORM matching USING 'PartNership' 'Partnership'.
*&---------------------------------------------------------------------*
*& Form matching
*&---------------------------------------------------------------------*
FORM matching USING a TYPE string
b TYPE string.
DATA lv_result TYPE string.
REPLACE ALL OCCURRENCES OF '*' IN a WITH '.*'.
FIND FIRST OCCURRENCE OF REGEX a IN b.
IF sy-subrc EQ 0.
lv_result = 'match'.
ELSE.
lv_result = 'no match'.
ENDIF.
WRITE:/(30) a,
(30) b,
lv_result.
ENDFORM. "matching