408 Stimmen

Alle Sonderzeichen, Satzzeichen und Leerzeichen aus der Zeichenkette entfernen

Ich muss alle Sonderzeichen, Satzzeichen und Leerzeichen aus einer Zeichenkette entfernen, so dass ich nur Buchstaben und Zahlen habe.

1voto

import re
my_string = """Strings are amongst the most popular data types in Python. We can create the strings by enclosing characters in quotes. Python treats single quotes the 

wie doppelte Anführungszeichen."""

# if we need to count the word python that ends with or without ',' or '.' at end

count = 0
for i in text:
    if i.endswith("."):
        text[count] = re.sub("^([a-z]+)(.)?$", r"\1", i)
    count += 1
print("The count of Python : ", text.count("python"))

0voto

Viren Ramani Punkte 63

Nach 10 Jahren habe ich unten geschrieben, dass es die beste Lösung ist. Sie können alle Sonderzeichen, Interpunktionszeichen, ASCII-Zeichen und Leerzeichen aus der Zeichenfolge entfernen/bereinigen.

from clean_text import clean

string = 'Special $#! characters   spaces 888323'
new = clean(string,lower=False,no_currency_symbols=True, no_punct = True,replace_with_currency_symbol='')
print(new)
Output ==> 'Special characters spaces 888323'
you can replace space if you want.
update = new.replace(' ','')
print(update)
Output ==> 'Specialcharactersspaces888323'

0voto

Art Bindu Punkte 521
function regexFuntion(st) {
  const regx = /[^\w\s]/gi; // allow : [a-zA-Z0-9, space]
  st = st.replace(regx, ''); // remove all data without [a-zA-Z0-9, space]
  st = st.replace(/\s\s+/g, ' '); // remove multiple space

  return st;
}

console.log(regexFuntion('$Hello; # -world--78asdf+-===asdflkj******lkjasdfj67;'));
// Output: Hello world78asdfasdflkjlkjasdfj67

-4voto

Dsw Wds Punkte 464
import re
abc = "askhnl#$%askdjalsdk"
ddd = abc.replace("#$%","")
print (ddd)

und Sie werden Ihr Ergebnis sehen als

'askhnlaskdjalsdk

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X