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

Contain Pattern issue

Former Member
0 Likes
2,085

Hi All,

I have a requirement where I need to replace the 'X' in the string.

EX: 1. ABCDXXX

2. ABXXCDXX

The output should be

1. ABCD

2. ABXXCD.

This mean I need to replace all the X which comes last in the string. There is a possiblity that X may come in between like Ex 2.

So that X should remain same.

What pattern I can give for this Or any other solution.

Kindly help.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,002

If you are on ABAP kernel release 7 or above, then you should be able to do the magic with below two lines of code. This shows power and beauty of regular expressions

Replace X in 'X+$' with any character you wish to delete towards the end of string. The regular expression matches only trailing X and it won't touch any X in the middle

DATA: ls_string TYPE string VALUE 'ABXXCDXX'.
REPLACE REGEX 'X+$' IN ls_string WITH ''.

If you are below ABAP 7 then you can use the more mundane SHIFT command.

DATA: ls_string TYPE string VALUE 'ABXXCDXX'.
SHIFT ls_string RIGHT DELETING TRAILING 'X'.
CONDENSE ls_string.

15 REPLIES 15
Read only

nabheetscn
SAP Champion
SAP Champion
0 Likes
2,002

Try the following code

l

_i = strlen( p_string ).
L_I = L_I - 1.
while ( l_flag <> 'X' ).
  if p_string+l_i(1) eq 'X'.
    p_string+l_i(1) = ''.
    l_i = l_i - 1.
  else.
    condense p_string.
    l_flag = 'X'.
  endif.
  ENDWHILE.

  write p_string.

Read only

Former Member
0 Likes
2,002

do a reverse of the string => shift left untill the following character not equals X => reverse again

Read only

Former Member
0 Likes
2,002

Hi Madan,

I tried implementing the logic you gave, but I am not getting proper results.

Here is code which I am working on , what can I write inside the LOOP so that the reults could be as told before:

TYPES: BEGIN OF ty_test,

col type char11,

END OF ty_test.

DATA: g_t_test type table of ty_test,

g_wa_test type ty_test.

DATA: len type i,

len1 type i.

g_wa_test-col = 'ABCDXXX'.

append g_wa_test to g_t_test.

clear: g_wa_test.

g_wa_test-col = 'ABXCDXXX'.

append g_wa_test to g_t_test.

clear: g_wa_test.

LOOP AT g_t_test INTO g_wA_test.

                  • ' What logic can we write here ' **********

WRITE:/ g_wa_test-col.

ENDLOOP.

Read only

0 Likes
2,002

Try this

LOOP AT g_t_test INTO g_wA_test.
l_i = strlen(  g_wA_test-col ).
L_I = L_I - 1.
while ( l_flag <> 'X' ).
  if g_wA_test-col+l_i(1) eq 'X'.
    g_wA_test-col+l_i(1) = ''.
    l_i = l_i - 1.
  else.
    condense g_wA_test-col.
    l_flag = 'X'.
  endif.
  ENDWHILE.
write / g_wA_test-col.
clear l_flag.
ENDLOOP.

Nabehet

Read only

Former Member
0 Likes
2,002

Hi Madan,

I tried implementing the logic you gave, but I am not getting proper results.

Here is code which I am working on , what can I write inside the LOOP so that the reults could be as told before:

TYPES: BEGIN OF ty_test,

col type char11,

END OF ty_test.

DATA: g_t_test type table of ty_test,

g_wa_test type ty_test.

g_wa_test-col = 'ABCDXXX'.

append g_wa_test to g_t_test.

clear: g_wa_test.

g_wa_test-col = 'ABXCDXXX'.

append g_wa_test to g_t_test.

clear: g_wa_test.

LOOP AT g_t_test INTO g_wA_test.

                  • ' What logic can we write here ' **********

WRITE:/ g_wa_test-col.

ENDLOOP.

Read only

Former Member
0 Likes
2,002

There is no operator in while ( l_flag 'X' ).

what operator should I use '='?

Suggest.

Read only

Former Member
0 Likes
2,003

If you are on ABAP kernel release 7 or above, then you should be able to do the magic with below two lines of code. This shows power and beauty of regular expressions

Replace X in 'X+$' with any character you wish to delete towards the end of string. The regular expression matches only trailing X and it won't touch any X in the middle

DATA: ls_string TYPE string VALUE 'ABXXCDXX'.
REPLACE REGEX 'X+$' IN ls_string WITH ''.

If you are below ABAP 7 then you can use the more mundane SHIFT command.

DATA: ls_string TYPE string VALUE 'ABXXCDXX'.
SHIFT ls_string RIGHT DELETING TRAILING 'X'.
CONDENSE ls_string.

Read only

0 Likes
2,002

while ( l_flag <> 'X' ).

Nabheet

Read only

Former Member
0 Likes
2,002

I am sorry Vishnu that didnt work.

Nabheet how can use the while ( l_falg 'X' ) like this in my code. Its a syntax error. But the procedure is correct.

Read only

0 Likes
2,002

Sumana, I gave you two pieces of code and clearly mentioned that the first will work only in ABAP 7 or above (that is ECC 6 or above).

The below piece of code should work for you.

DATA: ls_string TYPE string VALUE 'ABXXCDXX'.
SHIFT ls_string RIGHT DELETING TRAILING 'X'.
CONDENSE ls_string.

Also when you say some code doesn't work, it would be a better to mention what the error is, so we can help you.

Read only

Former Member
0 Likes
2,002

Sorry for that Vishnu. Actually there is no change in INPUT and OUTPUT both are same I am getting for the suggested code.

Read only

0 Likes
2,002

That is weird. Which SAP system are you on? Which release?

Both the pieces of code I gave you work on ECC 6 with ABAP 7

See if the below works.

ls_string should have 'ABXXCDXX' initially but after the SHIFT and CONDENSE it should become 'ABXXCD'

DATA: ls_string(30) TYPE c VALUE 'ABXXCDXX'.
SHIFT ls_string RIGHT DELETING TRAILING 'X'.
CONDENSE ls_string.

Read only

matt
Active Contributor
0 Likes
2,002

Basic programming question. Moved to Beginners' corner.

Read only

matt
Active Contributor
0 Likes
2,002

Basic programming question. Moved to Beginners' corner.

Moved back as resulted in interesting regex information...

Read only

Former Member
0 Likes
2,002

Hi Vishnu,

Thats really splved my problem.

The only thing which confused me was my field type was CHAR11 for which it didnt work, but when I changed the type to STRING your code became the solution for it.

Thank You.