爬虫笔记(9/25)------正则表达式

来源:互联网 发布:淘宝哪家鞋子质量好 编辑:程序博客网 时间:2024/05/10 06:24

1.正则表达式常用函数

1)re.match(pattern,string,flag)#(正则表达式,源字符,可选参数对应的标志位)

2)全局匹配函数

import restring="apythonhellomypythonhispythonourpythonend"pattern=re.compile(".python.")#预编译result=pattern.findall(string)#找出符合模式的所有结果print(result)
3)re.sub(pattern,rep,string,max)#(正则表达式,源字符,替换字符,最多替换次数)

实例:

正则表达式常用内容:

a.不能出现空格[^\s]*

b.以.com或者.cn结尾[.com|.cn]

c.任意的字母组合包括大小写[a-zA-Z]

d.匹配电子邮件\w+([.+-]\w+)*@\w+([.-]\w+)*\.\w+([.-]\w+)*

原创粉丝点击