Skip to Content
-3

How can I replace Special Character,numbers and months with space

Jun 05, 2017 at 10:59 AM

78

avatar image

I tried this but end up gating

/FC /FC /FC 121MAY1213 M/BT END XT 7.00XY 5.60AY 5.50YC 3.96XA 4.50XFEWR4.5 A/C 300.00

MBT END XT YR TU YQ XY AY YC XA XFEWR AC

MBT

but what i wanted is /FC /FC /FC 121MAY1213 M/BT END XT 7.00XY 5.60AY 5.50YC 3.96XA 4.50XFEWR4.5 A/C 300.00

END XT YR TU YQ XY AY YC XA XFEWR AC

END

DATA:


W_STR1 TYPE STRING VALUE '/FC /FC /FC 121MAY1213 M/BT END XT 7.00XY 5.60AY 5.50YC 3.96XA 4.50XFEWR4.5 A/C 300.00',
W_STRLEN TYPE I,
TXT1 TYPE STRING,
TXT2 TYPE STRING,
W_POS TYPE I,


W_VAR TYPE STRING.

CONDENSE W_STR1.
W_STRLEN = STRLEN( W_STR1 ).
WRITE:/ W_STR1.

DO W_STRLEN TIMES.

*search w_str1 for p_char.

IF SY-SUBRC EQ 0.

REPLACE ALL OCCURRENCES OF REGEX '[^a-zA-Z :]|(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)'
IN W_STR1 WITH ' ' IGNORING CASE.
* W_POS2 = W_POS2 + 1.
* W_POS3 = W_POS2 - 1.
* IF W_STR1+W_POS3(1) eq ' '.
* deleted = W_STR1+W_POS2(1).
* ENDIF.
W_VAR = W_STR1.
IF W_VAR CA SPACE.
W_POS = SY-FDPOS. "Getting offset
ENDIF.

W_VAR = W_VAR+0(W_POS).
IF STRLEN( W_VAR ) < 3 OR STRLEN( W_VAR ) > 3 .
REPLACE ALL OCCURRENCES OF W_VAR IN W_STR1 WITH SPACE IGNORING CASE.
CONDENSE W_STR1.
ENDIF.



ELSE.

WRITE:/ W_STR1.
WRITE: / W_VAR.

EXIT.

ENDIF.
ENDDO.

10 |10000 characters needed characters left characters exceeded

Could you please ask the question explicitly and clearly?

0

Dear Sandra,

hi

the code is working but its not adding space when it REPLACE ALL OCCURRENCES special char

with space

0

And your regex looks a bit suspect

0

the regex is meant to remove all special character, numbers and months, and its working fine

0
* Please Login or Register to Answer, Follow or Comment.

2 Answers

krishna chaitanya Jun 05, 2017 at 12:27 PM
0

Hi,

I don't Know whether this solution correct or not .

If we give 'space' after M/ .It is giving exact your output.

If it helps please check it.

Sorry, if i misleads.

Thanks,

Krishna.

Share
10 |10000 characters needed characters left characters exceeded
Sandra Rossi Jun 05, 2017 at 02:05 PM
0

Character strings (data objects of type C) with trailing spaces (and space alone too) are very tricky in ABAP. Often, the solution is to use the inverted quote ` ` (it's a text string, i.e. a data object of type STRING) instead of the single quote ' ' (it's a character string, i.e. a data object of type C).

Replace the code:

REPLACE ALL OCCURRENCES OF REGEX '[^a-zA-Z :]|(Jan(uary)?|Feb(ruary)?|Mar(ch)?'
& '|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?'
& '|Dec(ember)?)' IN W_STR1 WITH ' ' IGNORING CASE.

with this code:

REPLACE ALL OCCURRENCES OF REGEX '[^a-zA-Z :]|(Jan(uary)?|Feb(ruary)?|Mar(ch)?'
& '|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?'
& '|Dec(ember)?)' IN W_STR1 WITH ` ` IGNORING CASE.
Show 1 Share
10 |10000 characters needed characters left characters exceeded

In addition to the above code after replace statement condense w_str1 before assigning to w_var.Then the above statement will full your requirement.

0