立即学习:https://edu.csdn.net/course/play/24756/280712?utm_source=blogtoedu
1.findall:查找所有满足条件的
text="apple price is $99, orange price is $99"
result=re.findall(r'\$\d+',text)
print(result)
2.sub:根据规则替换其他字符串:
text="hello zhongguo, hello world"
new_text=re.sub(r' ,','\n',text)
print(new_text)
3.split:根据规则分割字符串:
text="hello zhongguo, hello world"
result=re.split(r' |,','\n',text)
print(result)输出{'hello', 'zhongguo', 'hello', 'world'}
4.compile:编译正则表达式:
text="apple price is 34.56"
r=re.compile(r'\d+\.?\d*')
result=re.search(r,text)
print(result.group())
5.正则表达式注释:("""
#..
#...
#...
""",re.VERBOSE)
如:
r=re.compile(r"""
\d+ #整数部分
\.? #小数点
\d* #小数部分
""",re.VERBOSE)