Application Development 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: 

Finding multiple occurance of a pattern in a String!!

rajesh_rajendran2
Participant
0 Kudos
746

Hi Experts.

I have a requirement to find the multiple occurance of a pattern in a string and then based on the result, I have to output a message.

I am enclosing a sample code:

<b>DATA : PATTERN TYPE C VALUE '.' ,

STRING (15) TYPE C VALUE '1456.2561.85'.</b>

<b>Now here I want to check the multiple occurance of '.' i,e the pattern.</b>

I know I can use Find and replace but I dont want to replace.

I want to raise a message when the pattern occurs more than once.

Is there any way to find the multiple occurance of the pattern in the string and set any counter accordingly. ( SY-SUBRC & SY-FDPOS don't help ).

Please guide !!!

WIth Regards,

Rajesh Rajendran

1 ACCEPTED SOLUTION

athavanraja
Active Contributor
0 Kudos
221
data: patt type string value `.`,
      text type string,
      off  type i,
      moff type i,
      mlen type i.
data: httpbody type string .
data: rcount type  i .

httpbody = 'Thi.sis.for.stest.' .
off = 0.
rcount = 0 .
while sy-subrc = 0.
  find patt in section offset off of
       httpbody " this is the variable where your text goes
       match offset moff
       match length mlen.
  if sy-subrc = 0.
    rcount = rcount + 1 .
    off = moff + mlen.
  endif.
endwhile.


the variable rcount will give the number of occurrences of '.' .

Regards

Raja

7 REPLIES 7

Former Member
0 Kudos
221

hi

try this code once n let me know whether it fulfills ur requirement .

DATA : PATTERN TYPE C VALUE '.' ,

STRING (15) TYPE C VALUE '1456.2561.85',

w_counter type i.

if string+w_pointer(1) = ' .'.

add 1 to w_counter.

endif.

write: w_counter.

regards,

kiran kumar k

0 Kudos
221

hi

try this code once n let me know whether it fulfills ur requirement .

DATA : PATTERN TYPE C VALUE '.' ,

STRING (15) TYPE C VALUE '1456.2561.85',

w_counter type i.

w_length = strlen( string ).

do .

if string+w_pointer(1) = ' .'.

add 1 to w_counter.

endif.

add 1 to w_pointer.

if w_ponter = w_length.

exit.

endif.

enddo.

write: w_counter.

athavanraja
Active Contributor
0 Kudos
222
data: patt type string value `.`,
      text type string,
      off  type i,
      moff type i,
      mlen type i.
data: httpbody type string .
data: rcount type  i .

httpbody = 'Thi.sis.for.stest.' .
off = 0.
rcount = 0 .
while sy-subrc = 0.
  find patt in section offset off of
       httpbody " this is the variable where your text goes
       match offset moff
       match length mlen.
  if sy-subrc = 0.
    rcount = rcount + 1 .
    off = moff + mlen.
  endif.
endwhile.


the variable rcount will give the number of occurrences of '.' .

Regards

Raja

Former Member
0 Kudos
221

Hi....

this can be done in multiple ways....

1) using split:

SPLIT <c> AT <b>'.'</b> INTO TABLE itab.

describe itab lines w_lin. " Find the number of records in itab.

if w_lin > 1.

write ur code to raise the message................ "

endif.

2)data str1(10),str2(10),str3(10).

SPLIT string AT <b>'.'</b> INTO str1 str2 str3.

if str3 is not null. " more than 1 <b>'.'</b>

write ur code to raise the message................

endif.

Reward points if useful.

Suresh......

Former Member
0 Kudos
221

Execute the code.

data : val(30) type c value '1456.2561.85'.
data : final  type i,
       cnt type i,
       v type c,
       n type i.
DATA : PATTERN TYPE C VALUE '.' .   " ur value for occurance


cnt = strlen( val ).

do cnt times.
move val+n(1) to v.
if v ca pattern.        "use this 
   final = final + 1.
endif.
n = n + 1.


enddo.

if final > 1.

message i001(zxx) with   'More than one occurance'.

endif.

regards,

vijay

Clemenss
Active Contributor
0 Kudos
221

Hi friends,

sorry if this looks a little academic. It shows the use of the newly-introduced and extremely useful classes cl_abap_regex and cl_abap_matcher and at the end a nice idea to give numeric output the length needed.


*&---------------------------------------------------------------------*
*&      Form  ANY
*&---------------------------------------------------------------------*
FORM ANY .
  DATA :
    lv_pattern                            TYPE string VALUE '.' ,
    lv_string                             TYPE string VALUE '1456.2561.85.78',
    lr_regex                              TYPE REF TO cl_abap_regex,
    lr_matcher                            TYPE REF TO cl_abap_matcher,
    lt_matches                            TYPE match_result_tab,
    lv_matchcount                         TYPE sytfill,
    lv_outlen type i.
  CREATE OBJECT lr_regex
    EXPORTING
      pattern                             = lv_pattern.
  CREATE OBJECT lr_matcher
    EXPORTING
      regex                               = lr_regex
      text                                = lv_string.
  lt_matches                              = lr_matcher->find_all( ).
  lv_matchcount                           = LINES( lt_matches ).
  lv_outlen                               = log10( lv_matchcount ) + 1.
  write: / lv_string, 'matches pattern', lv_pattern, at (lv_outlen) lv_matchcount, 'times'.
ENDFORM.                    " ANY

Have fun!

Regards,

Clemens

rajesh_rajendran2
Participant
0 Kudos
221

Hi Experts,

Thanks a lot for the numerous responses. My problem has been resolved and I have awarded suitable points.

With Regards,

Rajesh