vfc*_*sts 2 python regex arrays replace preg-replace-callback
根据http://www.php2python.com/wiki/function.preg-replace-callback/ re.sub是PHP的preg_replace_callback的python等价物,但php版本需要一个数组来匹配字符串,所以你可以传递多个字符串,但re.sub似乎只接受一个字符串.
这是对的还是我对python的微弱知识?
如果要在数组上执行此操作,可以使用列表推导,例如
>>> array_of_strings = ["3a1", "1b2", "1c", "d"]
>>> [re.sub("[a-zA-Z]", "", elem) for elem in array_of_strings]
["31", "12", "1", ""]
Run Code Online (Sandbox Code Playgroud)
虽然如果你使用复杂的表达式,你应该re.compile首先使用模式