1

How to remove special symbols /#$%^&*@0123456789 only if they are separated from each other by characters or symbols not in the list. For example:

H8e%&l6&%l@8095o a@/9^65$n228d w%e60$$#&9l3@/c6o5m3e --> Hello and welcome . #$%#^ --> . #$%#^ ,. a3%2%1/3$s*0. d8^! -->,. as. d! I1^/0^^@9t #$%% i/@4#s 11P17/9$M 5^&* a^$45$5$0n&##^4d 6^&&* I $%^$%^ a8@@94%3*m t3120i36&^1r2&^##0e&^d ---> It #$%% is 11PM 5^&* and 6^&&* I $%^$%^ am tired 

I know that simple string.replace will not work here. I tried something like this but it didn't work either:

 def _correct_message(message): f = re.match(r'[a-zA-Z][/#$%^&*@0123456789​][a-zA-Z]', message) if f is not None: message = re.sub('[/#$%^&*@0123456789​]', '', message) 

2 Answers 2

1

You can try this:

import re s = "H8e%&l6&%l@8095o a@/9^65$n228d w%e60$$#&9l3@/c6o5m3e" s1 = "I1^/0^^@9t #$%% i/@4#s 11P17/9$M 5^&* a^$45$5$0n&##^4d 6^&&* I $%^$%^ a8@@94%3*m t3120i36&^1r2&^##0e&^d" final_string = re.sub("(?<=[a-zA-Z\.\!])[/#\$\%\^\&\*\@0123456789]+(?=[a-zA-Z\.\!])", '', s) print(final_string) new_final_string = re.sub("(?<=[a-zA-Z\.\!])[/#\$\%\^\&\*\@0123456789]+(?=[a-zA-Z\.\!])", '', s1) print(new_final_string) print(re.sub("(?<=[a-zA-Z\.\!])[/#\$\%\^\&\*\@0123456789]+(?=[a-zA-Z\.\!])", '', ',. a3%2%1/3$s*0. d8^!')) 

Output:

'Hello and welcome' It #$%% is 11PM 5^&* and 6^&&* I $%^$%^ am tired ,. as. d! 
Sign up to request clarification or add additional context in comments.

4 Comments

How to make it in string? Because if I run it like this it outputs list of characters and symbols. s = ''H/2#e4$$&&0&l^2$l5o'' and print(final_string) will give ['H', '/', '2', '#', 'e', '4', '$', '$', '&', '&', '0', '&', 'l', '^', '2', '$', 'l', '5', 'o']
''.join(final_string)
Yes, I did join but is still didn't remove symbols
@Ajax1234 Thanks it almost work fine only ,. a3%2%1/3$s*0. d8^!' gives ,. as*0. d8^! but should be ,. as. d! And I want to clarify what means ?<= and why in the list of symbols some symbols are separated by "\"?
0

I know it is not exactly what you asked, but a first validation can be made based on .islapha() e.g:

 def validate(s): text='' for i in s: if i.isalpha(): text+=i else: pass return text 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.