简体   繁体   中英

remove special character from string, not replace them with space

I'm trying to remove special characters from a string. All the examples available only replaces them with space. But i want to get rid of them and retain the order of the string. Below are some codes which i tried

input_string = "abcd. efgh ijk L.M"
re.sub(r"[^a-zA-Z0-9]+",'',input_string).split(' '))) #approach 1
re.sub(r"[\W_]+",'',input_string).split(' '))) #approach 2

The desired output is

"abcd efgh ijk LM"

You can replace every special character (excluding space) by empty character:

re.sub(r"[^a-zA-Z0-9 ]+", '', input_string)

You can add an space after the caret( ^ ), like this

In [1]: re.sub(r"[^ a-zA-Z0-9]+",'',input_string)
#                  ^ space here
Out[1]: 'abcd efgh ijk LM'

If you also want to remove trailing or leading whitespaces you can use the strip method.

In [2]: '    Hello    '.strip()
Out[2]: 'Hello'

You can use the.replace() function.

not_wanted_char = [".", "*"]

your_string = "abcd. efgh ijk L.M"

for i in not_wanted_char:
    your_string = your_string.replace(i,"")
# initializing bad_chars_list
bad_chars = [';', ':', '!', "*"]
 
# initializing test string 
test_string = "he;ll*o w!or;ld !"
 
# printing original string 
print ("Original String : " + test_string)
 
# using replace() to 
# remove bad_chars 
for i in bad_chars :
    test_string = test_string.replace(i, '')
 
# printing resultant string 
print ("Resultant list is : " + str(test_string))

Original String: "he;ll*ow;or;ld !"

Resultant list is: "hello world"

If you want to keep spaces then below regex will work like a charm

import re
input_string = "abcd. efgh ijk L.M"
re.sub('[^ A-Za-z0-9]+', '', input_string)

It will result this string - abcd efgh ijk LM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM