个人空间网站,wordpress弹出框插件,大数据公司排名,邢台移动网站设计1、引入正则表达式
import re2、使用re.sub进行字符串替换
re.sub(pattern, repl, string, count0, flags0) 其中三个必选参数#xff1a;pattern, repl, string两个可选参数#xff1a;count, flagspattern #xff0c;表示正则中的模式字符串#xff0c;其中反斜杠加数…1、引入正则表达式
import re2、使用re.sub进行字符串替换
re.sub(pattern, repl, string, count0, flags0) 其中三个必选参数pattern, repl, string两个可选参数count, flagspattern 表示正则中的模式字符串其中反斜杠加数字\N则对应着匹配的组matched group 比如\6表示匹配前面pattern中的第6个group,意味着pattern中前面肯定是存在对应的第6个group然后你后面也才能去引用repl, 就是replacement被替换的字符串的意思。repl可以是字符串也可以是函数string 即表示要被处理要被替换的那个string字符串;count ,表示匹配pattern中被处理的匹配字符串个数
练习1
import re
inputStr hello crifan, nihao crifan, nihao ccc
match_strre.match(rhello (\w),inputStr) #\w匹配字母、数字、下划线
print(match_str.group(0))
print(match_str.group(1))
replacedStr re.sub(rhello (\w), nihao \1, crifanli, inputStr)
print(replacedStr)运行结果 hello crifan crifan hello crifan, nihao crifan, nihao ccc 练习2提取输入字符串中的数字
import re
optioninput()
option_strre.sub(\D,,option) #\D匹配非数字字符
print(option_str)运行结果 生活1生活2 12