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 for search string

Former Member
0 Likes
2,949

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.

15 REPLIES 15
Read only

Former Member
0 Likes
2,228

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.

Read only

0 Likes
2,228

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.

Read only

0 Likes
2,228

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.

Read only

0 Likes
2,228

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.

Read only

0 Likes
2,228

Tried code posted in previous comment ?

Read only

venkat_aileni
Contributor
0 Likes
2,228

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

Read only

Former Member
0 Likes
2,228

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.

Read only

asdasd_asdasd
Active Participant
0 Likes
2,228

This message was moderated.

Read only

Ruediger_Plantiko
Active Contributor
0 Likes
2,228

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

  • build a stringtab of all search words ( data: lt_terms type stringtab. ), and
  • loop over this stringtab, performing an ordinary find with each word,
  • If one of the find operations fails, leave the method with answer =  abap_false.
  • If you survived the loop, leave the method with answer = true.

Regards,

Rüdiger

Read only

0 Likes
2,228

Thanks Rüdiger

Read only

matt
Active Contributor
0 Likes
2,228

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!

Read only

former_member219162
Contributor
0 Likes
2,228

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.

Read only

Former Member
0 Likes
2,228

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.

Read only

0 Likes
2,228

Thanks Guys for your replies, I will try to find word by word instead of a clumsy Regex.

Read only

Ruediger_Plantiko
Active Contributor
0 Likes
2,228

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:

http://twiki.org/cgi-bin/view/Codev/TWikiPresentation2013x03x07?slideshow=on;skin=print;extralog=-+c...