re.
subn
(
pattern,
repl,
string,
count=0,
flags=0
)
Perform the same operation as sub()
, but return a tuple (new_string, number_of_subs_made)
.
Changed in version 3.1: Added the optional flags argument.
Changed in version 3.5: Unmatched groups are replaced with an empty string.
翻译:操作和sub类似,但是返回的是一个元组(新串,替换的次数)
import re string1 = "abcxxd" print(re.subn("(x*)","-",string1))
('-a-b-c--d-', 6)