接上篇文章,这篇文章说说在python中,正则表达式的使用,python提供了re模块来处理正则表达式
还是用天气预报的接口
{"reason":"查询成功!","result":{"city":"上海","realtime":{"temperature":"26","humidity":"94","info":"晴","wid":"00","direct":"北风","power":"0级","aqi":"65"},"future":[{"date":"2019-09-10","temperature":"25\/32℃","weather":"多云","wid":{"day":"01","night":"01"},"direct":"南风转东南风"},{"date":"2019-09-11","temperature":"25\/32℃","weather":"阴转多云","wid":{"day":"02","night":"01"},"direct":"东北风转东南风"},{"date":"2019-09-12","temperature":"25\/31℃","weather":"阴转多云","wid":{"day":"02","night":"01"},"direct":"东风转东南风"},{"date":"2019-09-13","temperature":"24\/31℃","weather":"多云","wid":{"day":"01","night":"01"},"direct":"东南风转东风"},{"date":"2019-09-14","temperature":"24\/29℃","weather":"小雨转多云","wid":{"day":"07","night":"01"},"direct":"东北风"}]},"error_code":0}
实例一:使用re.sub(pattern, repl, string, count=0, flags=0) 函数,将字符串的 ‘reason’替换成‘start’
参数讲解:
pattern :正则表达式
repl:需要替换的字符串,或者函数名,这里是可以自定义函数的
string :需要处理的字符串
count:需要替换的个数,0表示替换所有
比如:re.sub('reason','start',text,0),表示匹配text中所有符合‘reason’表达式的地方,并将其替换成‘start’
结果:
实例二:分别使用search(),match(),findall()方法查询接口中的日期信息
三个函数解释:
re.search(pattern, string, flags=0)
匹配整个字符串,找到一个合适的即停止
re.match(pattern, string, flags=0)match
从字符串开头匹配,如果开头没有匹配到,则返回None
re.findall(pattern, string, flags=0)findall
匹配整个字符串,并返回所有匹配结果
参数讲解:
pattern :正则表达式
string :需要处理的字符串
结果:
实例三: 只获取日期,不要date,例如:2019-09-10
方法一:
r=re.search('"date":"(.{10})"',text)
r.group(1)
group()函数是用来取search()函数和match()函数中match的值,可以看实例二的结果,search和findall返回结果格式是不同的
正则表达式中()代表一组字符的意思,group()函数中可以传递一个参数,这个参数就是第几组数据,0是返回所有,1是返回第一组数据,所以返回结果是:2019-09-10
方法二:
r = re.findall('"date":"(.{10})"',text)
这个方法会返回所有符合要求的日期