Skip to Content

How do i check if the string contains only set of patterns

Hi,

My requirement is that, i want to check if the string lv_string contains a patterns which are from the set (22, 33, 44).

for ex if lv_string = 2233443344, then it should satisfy the condition.

Basically, i want to check if the string contains ONLY any combination of the patterns (22,33,44).

I appreciate if any input for this problem..

Thanks,

Venky.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Dec 12, 2011 at 02:42 PM

    Hi Venkatesh,

    i think this sample code will help you out.

    you can declare three constants as:

    constants: c_str1 TYPE string VALUE '22',

    c_str2 TYPE string VALUE '33',

    c_str3 TYPE string VALUE '44'.

    IF lv_string CONTAINS c_str1 OR lv_string CONTAINS c_str2 OR lv_string CONTAINS c_str3.

    condition satisfies or whatever you want your code to do.

    ELSE condition not satisfied.

    I hope this will work in your case.

    Please revert back with feedback.

    Thanks,

    Anishek Kumar

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 12, 2011 at 11:49 AM

    Hello,

    You can use SEARCH <c> FOR <str> <options>. If you get your answer, please close the thread.

    Regards

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 12, 2011 at 11:52 AM

    Hi Venky,

    this looks like pattern-based matching with regular expressions.

    For your case, your expression could be like this: '(2|3|4|5)' what means: Take 22 or 33 or 44 or 55

    Simple programm, just type "1122334455" as text and "(2|3|4|5)" as pattern:

    parameters:
      p_text  type text255,
      p_patt  type text255.
    
    start-of-selection.
    
      field-symbols:
            <match> type match_result.
      data: lc_matcher type ref to cl_abap_matcher,
            lt_matches type match_result_tab.
    
    
      lc_matcher = cl_abap_matcher=>create( pattern     = p_patt
                                         ignore_case = abap_false
                                         text        = p_text ).
    
      lt_matches = lc_matcher->find_all( ).
    
      write: / 'Line       ', sy-vline, 'Offset     ', sy-vline, 'Length'.
      uline.
      loop at lt_matches assigning <match>.
        write: / <match>-line, sy-vline, <match>-offset, sy-vline, <match>-length.
      endloop.
    

    Kind regards,

    Hendrik

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Hendrik,

      Actually, I want to write a 'IF' condition statement for this algorithm:

      if lv_string contains any combination of values from the ONLY set (22, 33, 44).

      then, say: condition is satisfied.

      else. say: condition is not satisfied.

      endif.

      So, i need a code to write my first statement in the above code, which is "IF" statement.

      Thanks,

      Venky.

  • author's profile photo Former Member
    Former Member
    Posted on Dec 12, 2011 at 11:55 AM

    You can copy your string to another string and use REPLACE.

    str1 = '22334455'.

    move str1 to str2.

    replace all occurrences of '22' in str2 with ''.

    replace all occurrences of '33' in str2 with ''.

    replace all occurrences of '44' in str2 with ''.

    check the length of str2 using strlen.

    If the length = 0 then str1 contained only valid characters. If > 0, invalid characters were present.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.