python学习之re 18 subn(pattern, repl, string, count=0, flags=0)

re. subn ( patternreplstringcount=0flags=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)




猜你喜欢

转载自blog.csdn.net/rubikchen/article/details/80507685