python正则表达式

作者: dpengwang | 来源:发表于2018-05-18 16:29 被阅读0次

正则表示概念

1.使用单个字符串来描述匹配一系列符合某个语法规则的字符串

2.对字符串操作的一种逻辑模式

3.应用场景:处理文本和数据

4.正则表达式过程:依次拿出表达式和文本中的字符比较。如果每一个字符都能匹配成功。则匹配成功,否则匹配失效

要使用re模块

第一个正则表达式

pattern->compile -> match

输入:

import re
str ='imooc study hard'
pattern = re.compile(r'imooc',re.I)(re.I  忽略大小写)
result = pattern.match(str)
result.group()

结果

imooc

语法(通用):

匹配单个字符
字符 匹配
. 匹配任意字符(除了/n)
[...] 匹配字符集
\d / \D 匹配数字 /非数字
\s / \S 匹配空白/非空白字符
\w / \W 匹配单词字符[a-zA-Z0-9]/非单词字符
匹配多个字符
字符 匹配
* 匹配前一个字符0次或者无限次
+ 匹配前一个字符1次或者无限次
匹配前一个字符0次或者1次
{m}/{m,n} 匹配前一个字符m到n次
*/ +? / ?? 匹配模式变为非贪婪(尽可能少匹配字符)
边界匹配:
字符 匹配
$ 匹配字符串结尾
^ 匹配字符串开头
\A / \Z 指定的字符串必须出现在开头 /结尾(对于后面的生效)
| 匹配左右任意一个表达式
(ab) 括号中的表达式作为一个分组,返回的也是分组,而不是整个
\<number> 引用编号为num的分组匹配到的字符串(分组为上面的分组)
(?P<name>) 分组起一个别名
(?P=name) 引用别名为name的分组匹配字符串(与楼上一起用)

对于(ab)的说明:

test1:

str ='12aa23'
pattern= re.compile('[0-9]+aa[0-9]+')
result =re.findall(pattern,str)
print(result)

output

['12aa23']

test2:

str ='12aa23'
pattern= re.compile('([0-9]+)aa([0-9]+)')
result =re.findall(pattern,str)
print(result)

output

[('12', '23')]

re模块的方法

  1. match(pattern,str,flags=0) 仅从起始部分查找,也就是说第一个字符匹配不上,那一定返回的是空

  2. search(pattern,str,flags=0) 从str中查找返回对象

  3. findall(pattern ,str,flags=0) 返回一个list

  4. sub(pattern ,repl,string,count,flags=0) 将字符串中匹配正则表达式的部分替换为其他值(subn函数在sub基础上还返回替换的次数)

    str = 'daa100dad200'
    result =re.sub(r'\d+','**',str)
    print(str)
    print(result)
    

    output

    'daa100dad200'
    'daa**dad**'
    

    repl可以是一个函数

def add1(match):
val = match.group()
num = int(val) + 1
return str(num)
stra = 'daa100dad200'
result = re.sub(r'\d+', add1, stra)
print(result)

output

daa101dad201


​

5. split(pattern,string,maxsplit=0,flags=0)  根据匹配分割字符串,返回分割字符串组成的列表

str = 'imooc :C C++ JAVA Python' 分开
re.split(r':| ',str)


#### 简单练习:

```python
import urllib.request
import  re
req =urllib.request.urlopen('https://www.imooc.com/course/list?c=cb')
buf  = req.read()
buf =buf.decode('utf-8')
listurl  = re.findall(r'src=.+\.jpg',buf)
i=0
for url in listurl:
    f =open(r'E:/pythoncode/'+str(i)+'.jpg','ab')
    print(url[7:])
    req = urllib.request.urlopen(r'http://'+url[7:])
    buf  = req.read()
    f.write(buf)
    i+=1

相关文章

  • 正则表达式

    Python正则表达式初识(一) Python正则表达式初识(二) Python正则表达式初识(三) Python...

  • 正则表达式

    Python:正则表达式Python:正则表达式

  • Python正则表达式指南

    Python正则表达式指南 本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达...

  • Python爬虫(十)_正则表达式

    本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

  • python正则表达式

    本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

  • [转]python正则表达式(一) 函数使用

    原文:python | 史上最全的正则表达式 更全的正则表达式处理函数:在python中使用正则表达式(一) 0....

  • Python正则表达式

    python正则表达式

  • Python正则表达式用法详解

    搞懂Python 正则表达式用法 Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一...

  • Python正则表达式指南

    本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例...

  • Python处理正则表达式超时的办法

    title: Python处理正则表达式超时的办法tags: [python3, 正则表达式超时, re模块]da...

网友评论

    本文标题:python正则表达式

    本文链接:https://www.haomeiwen.com/subject/ntggdftx.html