‎2013 Oct 28 1:50 PM
Hi Experts,
I am trying to create a regular expression which can search for a combination of words in a string:
For example my search string is : "Find the right solutions to extend the value of your SAP software investments"
I want to search for "solutions" , "software" in this string such that these 2 words exist in the search string in any order , in any sequence, and only the matching words.
I cannot do this with a Find command as my search key words can be dynamic i.e 2 or 3 or more.
I searched for regex and found "|\software\b|\bsolutions\b|" but this is an OR condition and I want a and condition.
Can someone please advice.
‎2013 Oct 28 1:57 PM
Try with following code.
DATA lv_str TYPE string VALUE 'Find the right solutions to extend the value of your SAP software investments'.
IF lv_str CS 'solutions' and lv_str CS 'software'.
WRITE 'true'.
ELSE.
WRITE 'False'.
ENDIF.
‎2013 Oct 28 2:01 PM
PARAMETERS: lv_str TYPE string,
search1 TYPE string,
search2 TYPE string.
IF lv_str CS search1 AND lv_str CS search2.
WRITE 'true'.
ELSE.
WRITE 'False'.
ENDIF.
‎2013 Oct 28 2:04 PM
Hi Chandra,
I cannot use the CS operator because the search keywords can be 2 or 3 or more and determined at runtime dynamically.
User can input any kewords to search and hence I am trying to find out a regex.
‎2013 Oct 28 2:59 PM
DATA lv_str TYPE string VALUE 'Find the right solutions to extend the value of your SAP software investments'.
DATA:
ls_search TYPE string,
lt_search TYPE TABLE OF string,
lv_flag TYPE char1.
APPEND 'solutions' TO lt_search.
APPEND 'right' TO lt_search.
"APPEND 'right1' TO lt_search.
LOOP AT lt_search INTO ls_search.
CLEAR lv_flag.
IF lv_str CS ls_search.
lv_flag = 'X'.
ELSE.
CLEAR lv_flag.
EXIT.
ENDIF.
ENDLOOP.
IF lv_flag = 'X'.
WRITE 'Found'.
ELSE.
WRITE 'Not found'.
ENDIF.
‎2013 Oct 28 5:19 PM
‎2013 Oct 28 2:01 PM
Try this.
DATA: p_string TYPE string,
p_search1 TYPE string.
p_search1 = 'solutions'.
p_string = 'Find the right solutions to extend the value of your SAP software investments'.
SEARCH p_string FOR p_search1.
IF sy-subrc = 0.
WRITE p_search1.
ENDIF.
-Venkat
‎2013 Oct 28 2:05 PM
hello Bhanu,
Please find the logic below:
l_string = 'Find the right solutions to extend the value of your SAP software investments'.
FIND FIRST OCCURRENCE OF REGEX 'solutions' IN l_string.
if sy-subrc eq 0.
FIND FIRST OCCURRENCE OF REGEX 'software' IN l_string.
if sy-subrc eq 0.
"Arriving at this stage your variable l_string contains both the word solutions and software
"you can continue with your processing now in this block of code.
"and if you have dynamic text then loop in the internal table and continue the processing while
"filling the string variable.
endif.
endif.
Thanks and Kind Regards,
Yovish.
‎2013 Oct 28 2:07 PM
‎2013 Oct 28 2:14 PM
The former replies didn't read or ignored the condition
"in any sequence, any order"
in the question
If you solve this with regexes, the and condition you are looking for is simply writing the search terms one after the other. But since you want it in any order, you have two expressions for the two possible orders:
'\bsoftware\b.*\bsolutions\b|\bsolutions\b.*\bsoftware\b'
Unfortunately, this gets clumsy if you say you want to have even more search strings. In this case I would
Regards,
Rüdiger
‎2013 Oct 28 3:21 PM
‎2013 Oct 28 2:14 PM
I suggest that you post this on a forum that has expertise in Regex. I use Regex fairly regularly, but for complex stuff, I have my own expert to ask.
It's unlikely you'll find the expertise here - as you can tell by the number of people offering CS as a solution.
I'd also suggest that when you get the answer you post it here!
‎2013 Oct 28 2:15 PM
Hello Bhanu,
Try this code;
DATA: l_text TYPE string.
PARAMETERS: l_srch TYPE string.
DATA: BEGIN OF l_word OCCURS 0,
word(50),
END OF l_word.
l_text = 'Find the right solutions to extend the value of your SAP software investments'.
*-- whatever text you give in parameter is broken into words
*-- and each word is stored in a line of table.
SPLIT l_srch AT ' ' INTO TABLE l_word.
*-- if user has given extra spacing between words
*-- then delete the extra lines which does not have any word to compare.
Delete l_word where word is initial.
*-- loop for each word and search the word in the string
*-- then do your own processing.
LOOP AT l_word.
SEARCH l_text FOR l_word-word.
if sy-subrc = 0.
"This means the word is present in string
else.
"word is not present in string.
endif.
Endloop.
‎2013 Oct 28 2:41 PM
When you say irrespective of order, with a condition that all words have to appear in string, you are looking for an irregular expression instead of a regular one.
For 4 words, there are 24 permutations possible (4P4).
You could put loop and use regex to find every possible permutation, or combine all permutations to single regex which would be ugly.
For 10 words, you just have to loop through word list and look for their occurrence, setting false flag on first failed search. Regex isn't really required.
Please go with Rüdiger's reply.
‎2013 Oct 28 3:22 PM
Thanks Guys for your replies, I will try to find word by word instead of a clumsy Regex.
‎2013 Nov 01 7:03 PM
Just an addendum for the logical AND question in regex: Since ABAP regexes support the non-capturing lookahead assertion, the following regex would do it
(?=.*solutions)(?=.*software)
Compare for example Peter Thoeny's recent presentation on regexes: