Python中如何使用re.sub进行字符串替换操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计109个文字,预计阅读时间需要1分钟。
pythonstr1=2017-10-15 this is happy day...str1=re.sub(r(\d{4})-(\d{2})-(\d{2}), r\1/\2/\3, str1)
str1="2017-10-15 this is happy day...">>> re.sub("(\d{4})-(\d{2})-(\d{2})",r"\2/\3/\1",str1)
##'10/15/2017 this is happy day...'
>>> re.sub("(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})",r"\g<month>/\g<day>/\g<year>",str1)
#'10/15/2017 this is happy day...'
本文共计109个文字,预计阅读时间需要1分钟。
pythonstr1=2017-10-15 this is happy day...str1=re.sub(r(\d{4})-(\d{2})-(\d{2}), r\1/\2/\3, str1)
str1="2017-10-15 this is happy day...">>> re.sub("(\d{4})-(\d{2})-(\d{2})",r"\2/\3/\1",str1)
##'10/15/2017 this is happy day...'
>>> re.sub("(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})",r"\g<month>/\g<day>/\g<year>",str1)
#'10/15/2017 this is happy day...'

