其他分享
首页 > 其他分享> > re模块

re模块

作者:互联网

re模块

用法 说明
str.replace(old, new[, max]) replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
re.sub(’\d+’,‘666’,inputStr) 将inputStr中的数字替换成666

\d:匹配任意一个数字 包括[0-9]和其他数字字符
\D:(\d)的相反

import re

inputStr = 'hello 123 world 456'
replaceStr = inputStr.replace('123','321')
print(replaceStr)

replaceStr = re.sub('\d+','666',inputStr)
print(replaceStr)
replaceStr = re.sub('\D+','----',inputStr)
print(replaceStr)

在这里插入图片描述

_nigar 发布了86 篇原创文章 · 获赞 22 · 访问量 3108 私信 关注

标签:sub,replaceStr,666,replace,re,模块,inputStr
来源: https://blog.csdn.net/nigar_/article/details/104116160