tags:

views:

188

answers:

3

If my string was:

Business -- way's

I'd like to turn this into:

Business  ways

ie. replace NON abc/123 into ""

+8  A: 

Simple regular expression:

import re

>>> s = "Business  -- way's"
>>> s = re.sub(r'[^\w\s]', '', s)
>>> s
"Business  ways"
Triptych
+4  A: 

Or, if you don't want to use a regular expression for some reason:

''.join([x for x in foo if x.isalpha() or x.isspace()])
DisplacedAussie
A: 

(regular expression) replace

[[:punct:]]

with '' (if Python supports that).

[] is a character class, [::] is posix class syntax. [:punct:] is punctuation, so the character class for all punctuation marks would be [[:punct:]]

An alternate way of the same thing is \p and friends: \p{IsPunct}

See just below "Character Classes and other Special Escapes" in http://perldoc.perl.org/perlre.html (yes, I know it's a Perl document, but this is more about regular expressions than Perl).

That being said, the first answer with [^\w\s] answers what you explained a little more explicitly. This was more just an explanation of how to do what your question asked.

genio
python does not offer that type of pre-defined character class. The perl documentation doesn't help in this case.
JimB