I do not like regular expressions, but I like Python, so I will probably write this as
>>> s = ':foo [bar]' >>> ''.join(c for c in s if c.isalnum() or c.isspace()) 'foo bar' >>> ''.join(c for c in s if c.isalnum() or c.isspace()).split() ['foo', 'bar']
The idiom ".join" is a bit strange, I admit, but you can almost read the rest in English: "append each character to the characters in s if the character is alphanumeric or the character is a space, then separate that."
Alternatively, if you know that the characters you want to delete will always be outside, and the word will still be separated by spaces, and you know what it is, you can try something like
>>> s = ':foo [bar]' >>> s.split() [':foo', '[bar]'] >>> [word.strip(':[]') for word in s.split()] ['foo', 'bar']
source share