''' 正则 ''' import re text = 'I Love Python, I also love C#' r = r'[Ll]ove' ''' findall()和finditer()函数非常相似,它们的区别如下所示 findall():在输入字符串中查找所有匹配内容,如果匹配成功,则返回match列表对象,如果匹配失败则返回None finditer():在输入字符串中查找所有匹配内容,如果匹配成功,则返回容纳match的可迭代对象,通过迭代对象每次可以返回一个match对象,如果匹配失败则返回None ''' m = re.findall(r, text) if m is not None: print(m) ''' re.split(pattern, string, maxsplit=0, flags=0) re.sub(pattern, rep1, string, count=0, flags=0) ''' text1 = 'aa12bb34cc56dd' r1 = r'\d+' print(re.split(r1, text1)) print(re.sub(r1, ' ', text1))
评论
评论功能已经关闭!