Chris Bunch Chris Bunch - 1 year ago 93
Python Question

Python - Use a Regex to Filter Data

Is there a simple way to remove all characters from a given string that match a given regular expression? I know in Ruby I can use


>> key = "cd baz ; ls -l"
=> "cd baz ; ls -l"
>> newkey = key.gsub(/[^\w\d]/, "")
=> "cdbazlsl"

What would the equivalent function be in Python?

Answer Source
import re
re.sub(pattern, '', s)


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download