Remove some characters from a string
fredrik at pythonware.com
Thu Jul 17 11:13:26 CEST 2008
> I can't seem to find the right regular expression to achieve what I
> want. I'd like to remove all characters from a string that are not
> numbers, letters or underscores.
> For example:
>>>> magic_function('si_98%u^d at .as-*gf')
> str: 'si_98udasgf'
the easiest way is to replace the things you don't want with an empty
>>> re.sub("\W", "", "si_98%u^d at .as-*gf")
("\W" matches everything that is "not numbers, letters, or underscores",
where the alphabet defaults to ASCII. to include non-ASCII letters, add
"(?u)" in front of the expression, and pass in a Unicode string).
More information about the Python-list