Remove some characters from a string
Fredrik Lundh
fredrik at pythonware.com
Thu Jul 17 05:13:26 EDT 2008
Julien wrote:
> 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
string:
>>> re.sub("\W", "", "si_98%u^d at .as-*gf")
'si_98udasgf'
("\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).
</F>
More information about the Python-list
mailing list