Visa De
Visa De

Reputation: 21

Delete unknown special character

Delete special character

 s="____Ç_apple___   _______new A_____"

 print(re.sub('[^0-9a-zA-Z]\s+$', '', s))

result = ____Ç_______________apple___ _______new A_____

 s="____Ç_apple___   _______new A_____"

 print(re.sub('[^0-9a-zA-Z]', '', s))

result= applenewA

final result = apple new A

but i cannot get it

i want to delete Ç and _ and maintain space and English

Upvotes: 0

Views: 39

Answers (2)

blhsing
blhsing

Reputation: 106455

Since you want to consolidate multiple spaces into one space, and then remove characters that are not words or spaces, you should do it in two separate regex substitutions:

print(re.sub(r'[^0-9a-zA-Z ]+', '', re.sub(r'\s+', ' ', s)))

This outputs:

apple new A

Upvotes: 3

Yuda
Yuda

Reputation: 343

You want 'apple new A' for the result, right?

s="____Ç_apple___   _______new A_____"

result = re.sub('[^a-zA-Z|\s]+', '', s)  # apple   new A
result = ' '.join(result.split())  # apple new A
print(result)

Upvotes: 0

Related Questions