立即学习:https://edu.csdn.net/course/play/6861/326749?utm_source=blogtoedu
例:
import re
#取出字符串中所有的天气状态
string1 ="{ymd:'2018-01-01',tianqi:'晴',aqiInfo:'轻度污染'},{ymd:'2018-01-02',tianqi:'阴~小雨',aqiInfo:'优'},{ymd:'2018-01-03',tianqi:'小雨~中雨',aqiInfo:'优'},{ymd:'2018-01-04',tianqi:'中雨~小雨',aqiInfo:'优'}"
print(re.findall("tianqi:'(.*?)'",string1))
.*? 任意 >=0 ? 非贪/匹配一次或0次
import re
#取出所有含O字母的单词
string2 = 'Together ,we dscovered the a free market only thrives when there are rules to ensure competition and fair play,Our celebration of initiative and enterprise'
print(re.findall('\w*o\w*',string2,flags=re.I))
#删除字符串中的标点符号,数字和字母
import re
string3= '据悉,这次发动的4台蒸汽机冷凝管属于国际热核聚变实验堆项目的核二级压力设备,先后完成了压力试验,真空试验,氦气捡漏试验,千斤顶试验,吊耳载荷试验,叠装试验等验收试验'
print(re.sub('[,.、a-z]A-Z0-9()]','',string3))
#将每一部分的内容分隔开
import re
string4 = '2室2厅|101.62平|低区/7层|朝南\n 上海未来-浦东-金杨-2005年建'
split= re.split('[-\|\n]',string4)
print("split")
| 或
\| = |