有没有一种简单的方法可以从给定字符串中删除与给定正则表达式匹配的所有字符?我知道在 Ruby 中我可以使用gsub
:
>> key = "cd baz ; ls -l"
=> "cd baz ; ls -l"
>> newkey = key.gsub(/[^\w\d]/, "")
=> "cdbazlsl"
Python 中的等效函数是什么?
import re
re.sub(pattern, '', s)
Docs http://docs.python.org/library/re.html#re.sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)