版权声明:本文为作者原创文章,可以随意转载,但必须在明确位置表明出处!!!
为什么要学习正则表达式
处理文本和数据是件大事,也是我们日常工作中占比较多的一部分,文字处理、网页填表、来自数据库的信息流、股票报价信息、新闻列表等等。但是因为我们可能不知道这些需要计算机编程处理文本或数据的具体内容,所有把这些文本或数据以某种被计算机识别和处理的模式表达出来是非常有用的,而正则表达式(高级文本模式匹配)可以通过一些字符和特殊符号组合成能被计算机识别的模式在文本数据中去匹配我们定义的字符串集合。python的爬虫是离不开正则表达式的,如果正则表达式学不好,那么爬虫肯定是学不好的,爬虫学不好怎么去爬妹子的信息,怎么去给女朋友提高工作效率,怎么去抢票…
正则表达式使用的特殊符号和字符
python的正则表达式模块re模块
re模块主要的函数和方法如下:
- compile(pattern, flags=0)
对正则表达式模式pattern进行编译,flags可选标志,具体的可选标志可以查看api文档,通常情况下使用re.S(除换行符外匹配所有字符)函数返回一个regex对象 - match(pattern, string, flags=0)
在字符串string中匹配表达式模式pattern, 如果成功匹配则返回一个匹配对象,否则返回None - search(pattern, string, flags=0)
在字符串string中查找正则表达式模式pattern的第一次出现,flags可选标志,如果成功匹配则返回一个匹配对象,否则返回None - findall(pattern, string[,flags])
在字符串string中查找正则表达式pattern的所有(非重复)出现,返回一个匹配对象的列表 - finditer(pattern, string[,flags])
和findall()相同,但返回的不是列表而是迭代器;对于每个匹配,该迭代器返回一个匹配对象 - split(pattern, string, max=0)
根据正则表达式pattern 中的分隔符把字符string 分割为一个列表,返回成功匹配的列表,最多分割max 次(默认是分割所有匹配的地方)。 - sub(pattern, repl, string, max=0)
把字符串string 中所有匹配正则表达式pattern 的地方替换成字符串repl,如果max 的值没有给出,则对所有匹配的地方进行替换 - group(num=0)
返回全部匹配对象(或指定编号是num 的子组) - groups()
返回一个包含全部匹配的子组的元组(如果没有成功匹配,就返回一个空元组)
实战
- 记号literal使用12345678910111213141516171819202122232425262728293031323334353637383940414243import recontent = '''Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text orthe code—we would be grateful if you could report this to us. By doing so, you cansave other readers from frustration and help us improve subsequent versions of thisbook. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata,'''# literal 匹配字符串的值pattern = re.compile('find', re.S)# 使用findall函数,该函数返回一个列表items = re.findall(pattern, content)print('findall() 返回一个列表: %s' % items)print('*'*100)# 使用finditer函数,该函数返回一个迭代器,迭代器是一个match对象,需要用match对象的group()函数取值items = re.finditer(pattern, content)print('finditer()返回一个迭代器:%s' % items)for index, item in enumerate(items):print('item[%d] = %s, item-value = %s' % (index, item, item.group()))print('*'*100)# 使用match函数, 该函数返回一个match对象, 该函数只返回一个匹配的对象,切记!切记!# 这个函数只检查正则表达式是不是在string的开始位置匹配,所以下面的表达式返回的是Noneitems = re.match(pattern, content)print(items)print('*'*100)# 使用search, 该函数返回表达式模式pattern的第一次出现, 同时该函数返回一个match对象item = re.search(pattern, content)print('item = %s, item-value = %s' % (item, item.group()))print('*'*100)# sub替换字符串中pattern匹配的地方, 下面是将字符串中的find替换成helloitem = re.sub(pattern, 'hello', content)print(item)print('*'*100)pattern = re.compile('find|we', re.S)# 使用findall函数,该函数返回一个列表items = re.findall(pattern, content)print('findall() 返回一个列表: %s' % items)print('*'*100)
执行结果
|
|
正则表达是的综合运用
|
|
执行结果
|
|
note:不管是学习爬虫还是运用到其它文本处理方面正则表达是都是我们不得不学习的东西,正则表达是需要多用,长时间不用很快就会忘记,还有最重要的一点如果你正在读这篇文章请记得一定要自己去实践,只有实践你才回发现问题。正则表达式没有通用的表达式,只有适合的表达式,一种匹配可已有不同的正则表达式,正所谓不管是白猫还是黑猫,只要能抓到老鼠都是好猫。