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

ABAP Regular Expression

former_member813607
Contributor
0 Likes
1,016

Hi All,

Can someone help me with a regular expression to match and return a pattern from a string

The pattern is anything within parenthesis and three characters long!

Eg: (B6P)

That is I have to get the first matching from the string

Eg2.

From string 'ADFGDFG(4GH)ghjghj(HH6)FGHghfhgfGFHJ(DFGFG)GFHFG sdfg dfgdf

I need to get (4GH)

Eg3

From String 'SDFG(SD6GD)FGDFGDFGsdfgdfg(ghj)(fghgffh)gfhgfhgfDFGDFHG

I need to get (ghi)

Can someone help?. Any quick help will be appreciated heavily

Thanks, Sudeep..

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
919

Hi Sudeep,

This should be match your requirment:

DATA: str TYPE string.

  str = 'SDFG(SD6GD)FGDFGDFGsdfgdfg(ghj)(fghgffh)gfhgfhgfDFGDFHG'.
  FIND FIRST OCCURRENCE OF REGEX
   '\(.{3}\)'
    IN str
    IGNORING CASE
    MATCH OFFSET moff
    MATCH LENGTH mlen.

  WRITE:str+moff(mlen).

Regards,

Archer

7 REPLIES 7
Read only

Paulo_Vantini
Participant
0 Likes
919

Hi Sudeep,

for string 'ADFGDFG(4GH)ghjghj(HH6)FGHghfhgfGFHJ(DFGFG)GFHFG sdfg dfgdf


just create a string variable with name v_string and use string manipulation like:



DATA: v_string TYPE string,

            v_result TYPE string.


v_string = 'ADFGDFG(4GH)ghjghj(HH6)FGHghfhgfGFHJ(DFGFG)GFHFG sdfg dfgdf'.

v_result = v_string+7(5).

WRITE: v_result.


and for string 'SDFG(SD6GD)FGDFGDFGsdfgdfg(ghj)(fghgffh)gfhgfhgfDFGDFHG' do the same couting the characters and setting the length of the output.


Hope it solves your issue.

Regards.

Read only

0 Likes
919

Paulo,

I am looking for a generic solution,, not for the example

Regards, Sudeep..

Read only

0 Likes
919

Sudeep,

you can use that logic to build a subroutine for your problem like:

REPORT  Z_STRING_MANIPULATION.

PARAMETERS: p_string TYPE string,

                          p_count TYPE I,

                          p_length TYPE I.

PERFORM z_return_string USING p_count p_length CHANGING p_string.

WRITE: p_string.

FORM z_return_string USING p_count TYPE I

                                             p_length TYPE I

                                  CHANGING p_return TYPE string.

   p_return = p_return+p_count(p_length).

ENDFORM.



other solution ( if I got your picture ) is as follows:


REPORT  Z_STRING_MANIPULATION.


DATA: pos TYPE i,

       v_dif TYPE i,

       v_return TYPE string,

       pos_2 TYPE i.

PARAMETERS: p_text TYPE string DEFAULT 'SDFG(SD6GD)FGDFGDFGsdfgdfg(ghj)(fghgffh)gfhgfhgfDFGDFHG'.

DO.

SEARCH p_text FOR '('.

IF sy-subrc = 0.

   pos = sy-fdpos.

ENDIF.

SEARCH p_text FOR ')'.

IF sy-subrc = 0.

   pos_2 = sy-fdpos.

ENDIF.

v_dif = pos_2 - pos.

IF  v_dif = 4.

v_return = p_text+pos(5).

WRITE: v_return.

exit.

ELSE.

   pos_2 = pos_2 + 1.

   p_text = p_text+pos_2.

ENDIF.

ENDDO.

Regards.

Read only

Former Member
0 Likes
920

Hi Sudeep,

This should be match your requirment:

DATA: str TYPE string.

  str = 'SDFG(SD6GD)FGDFGDFGsdfgdfg(ghj)(fghgffh)gfhgfhgfDFGDFHG'.
  FIND FIRST OCCURRENCE OF REGEX
   '\(.{3}\)'
    IN str
    IGNORING CASE
    MATCH OFFSET moff
    MATCH LENGTH mlen.

  WRITE:str+moff(mlen).

Regards,

Archer

Read only

0 Likes
919

Archer

That's exactly what I was looking for brother, you solved my problem!!

Thanks, Sudeep..

Read only

0 Likes
919

Regex may need to be modified of your string can have this type of data.

For input string 'a(bc))ef(ghi)' , first match would be (bc)) , not (ghi).

See this snippet. Between parenthesis, we need to ensure that there are no parenthesis.

DATA: str     TYPE string,

      result  TYPE string.

str = 'asdf(asdf)asdf(fds)abc'.

FIND REGEX '(\([^()]{3}\))' IN str SUBMATCHES str.

WRITE: sy-subrc, result.

str = 'a(bc))ef(ghi)'.

FIND REGEX '(\(.{3}\))' IN str SUBMATCHES result.

WRITE: sy-subrc, result.

Read only

0 Likes
919

Yes, you are right. Anyway, this decide by what kind of data he have.