1

In a sentence, I want to remove a complete string which contains special characters.

Input is ["i am in jersey1&2","this code is %bdn2*nn"]

Expected output is ["i am in","this code is"]

import re Dsp = ["i am in jersey1&2","this code is %bdn2*nn"] Dsp1 = [] for i in Dsp: Dsp = [re.sub('^\W+$'," ",j) for j in i] Dsp1.append("".join(Dsp)) 

I got

Dsp1 = ["i am in jersey12","this code is bdn2nn"] 

but the expected output is

Dsp1 = ["i am in", "this code is"] 
1
  • thats because when you do for i in Dsp: it will iterate through all the characters in that sentence one by one, that means it wont consider individual string at i for every iteration. Why dont you try to split the sentence with delimiter as " " (blank space) then you will have a list of individual strings. Then you can check individual strings and remove the ones you dont need. Commented May 29, 2019 at 6:13

2 Answers 2

5

Using str.isalpha

Input = ["i am in jersey1&2","this code is %bdn2*nn"] for i in Input: print(" ".join(j for j in i.split() if j.isalpha())) 

Output:

i am in this code is 
Sign up to request clarification or add additional context in comments.

Comments

3

You're expecting re.sub to match and replace on word level, where words are substrings delimited by whitespace within a string. Regular expressions will match on a character level, unless you manage to tell them otherwise (which is a bit complicated) or you split the strings on whitespace before applying a special-character matching regex:

Dsp = ["i am in jersey1&2","this code is %bdn2*nn"] Dsp1 = [] for sentence in Dsp: cleaned_sentence = [] for word in sentence.split(' '): if not re.search(r'\W'): cleaned_sentence.append(word) DSP1.append(' '.join(cleaned_sentence)) print(DSP1) # ['i am in', 'this code is'] 

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.