Skip to content

Commit 299aaa0

Browse files
正则表达式
学习了正则表达式 并制作了一个简单的爬图
1 parent 2a17cb8 commit 299aaa0

File tree

11 files changed

+840
-45
lines changed

11 files changed

+840
-45
lines changed

.idea/workspace.xml

Lines changed: 28 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

regularExpression/.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

regularExpression/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

regularExpression/.idea/regularExpression.iml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

regularExpression/.idea/workspace.xml

Lines changed: 466 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

regularExpression/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 正则表达式
2+
1. 使用单个字符串来描述匹配一系列符合某个句法规则的字符串。
3+
2. 是对字符串操作的一种逻辑公式
4+
3. 应用场景:处理文本和数据
5+
4. 正则表达式过程:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配, 则匹配成功;否则匹配失败。
6+
7+
## Python下使用正则表达式
8+
1. `import re`:正则表达式模块:Python通过re模块提供对正则表达式的支持。使用re的一般步骤是先将正则表达式的字符串形式编译为Pattern实例,然后使用Pattern实例处理文本并获得匹配结果(一个Match实例),最后使用Match实例获得信息,进行其他的操作。r代表进行匹配的是元字符串, 不使用元字符串则需要注意转译的情况。
9+
10+
使用正则表达式匹配字符串开头是否为指定的字符或字符串:
11+
12+
13+
```
14+
import re
15+
str1 = 'test python'
16+
# 将正则表达式编译成pattern对象
17+
# 使用r'test', r代表进行匹配的是元字符串
18+
pa = re.compile(r'test') # pa已经成为一个pattern实例
19+
print(type(pa))
20+
21+
ma = pa.match(str1) # 若匹配成功, ma成为一个match对象
22+
23+
print(ma)
24+
print(ma.group()) # group()返回一个str或者tuple
25+
print(ma.span()) # 返回字符串的索引
26+
print(ma.re) # pattern的实例
27+
```
28+
29+
```
30+
# 返回结果
31+
<class '_sre.SRE_Pattern'>
32+
<_sre.SRE_Match object; span=(0, 4), match='test'>
33+
test
34+
(0, 4)
35+
re.compile('test')
36+
```
37+
38+
## 正则表达式语法
39+
40+
41+
|字符|匹配|
42+
|------|------|
43+
|.|匹配任意字符(除了\n)|
44+
|[...]|匹配字符集|
45+
|\d / \D|匹配数字/非数字|
46+
|\s / \S|匹配空白/非空白字符|
47+
|\w / \W|匹配单词字符[a-zA-Z0-9]/非单词字符|
48+
|\*|匹配前一个字符0次或者无限次|
49+
|+|匹配前一个字符1次或者无限次|
50+
||匹配前一个字符0次或1次|
51+
|{m}/{m,n}|匹配前一个字符m次或者n次|
52+
|{m,}|匹配前一个字符m次或更多次|
53+
|\*? / +? / ??|匹配模式变为非贪婪(尽可能少匹配字符|)
54+
|^|匹配字符串开头|
55+
|$|匹配字符串结尾|
56+
|\A / \Z|指定的字符串匹配必须出现在开头/结尾|
57+
|\||匹配左右任意一个表达式|
58+
|(ab)|括号中表达式作为一个分组|
59+
|\\\<number>|引用编号为num的分组匹配到的字符串|
60+
|(?P\<name>)|分组起一个别名|
61+
|(?P=name)|引用别名为name的分组匹配字符串|
62+
63+
## Python正则表达式--re模块其他方法
64+
1. search(pattern, string, flags=0)在一个字符串中查找匹配
65+
2. findall(pattern, string, flags=0)找到匹配,返回所有匹配部分的列表
66+
3. sub(pattern, repl, string, count=0, flags=0)将字符串中匹配正则表达式的部分替换为其它值
67+
4. split(pattern, string, maxsplit=0, flags=0)根据匹配分割字符串, 返回分割字符串组成的列表
68+
69+
## re练习
70+
利用正则表达式尝试抓取了20160711[腾讯网](http://www.qq.com/)上的jpg和png图像。

regularExpression/reTest.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
str1 = 'test python'
2+
# 未使用正则表达式的查找
3+
print(str1.find('1'))
4+
print(str1.find('test'))
5+
print(str1.startswith('test'))
6+
7+
# 使用正则表达式查找
8+
import re
9+
# 将正则表达式编译成pattern对象
10+
# 使用r'test', r代表进行匹配的是元字符串
11+
pa = re.compile(r'test') # pa已经成为一个pattern实例
12+
print(type(pa))
13+
14+
ma = pa.match(str1) # 若匹配成功, ma成为一个match对象
15+
16+
print(ma)
17+
print(ma.group()) # group()返回一个str或者tuple
18+
print(ma.span()) # 返回字符串的索引
19+
print(ma.re) # pattern的实例
20+
21+
# 另一个例子
22+
pa2 = re.compile(r'_')
23+
ma2 = pa2.match('_value')
24+
25+
print(ma2.group())
26+
27+
# 例子3
28+
pa3 = re.compile(r'_')
29+
ma3 = pa3.match('value_')
30+
# print(ma3.group()) #匹配失败 'NoneType' object has no attribute 'group'
31+
32+
# 忽略大小写匹配
33+
pa = re.compile(r'test', re.I) # re.I 忽略大小写, I=ignore
34+
print(pa)
35+
36+
ma = pa.match('Test python')
37+
print(ma.group())
38+
39+
#
40+
ma = re.match(r'test', 'Test Python', re.I)
41+
print(ma.group())
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import re
2+
3+
str1 = 'study time = 1000'
4+
5+
print(str1.find('1000'))
6+
7+
# 如果数字1000发生变化, 可以使用正则表达式的search查找数组, 其中\d+就是匹配任意多字符的数字
8+
# search仅能查找第一个符合项
9+
info = re.search(r'\d+', str1)
10+
print(info.group())
11+
12+
str1 = 'study time = 10000'
13+
info = re.search(r'\d+', str1)
14+
print(info.group())
15+
16+
# findall 能查找到所有匹配项, 返回值为一个list
17+
str2 = 'python code num = 100, swift code num = 50, c++ code num = 10'
18+
info = re.findall(r'\d+', str2)
19+
print(info)
20+
print(sum([int(x) for x in info]))
21+
22+
# sub将字符中匹配正则表达式的部分替换为其它值
23+
str3 = 'python num = 1000'
24+
info = re.sub(r'\d+', '1001', str3) # 将str3中的数字替换为1001
25+
print(info)
26+
27+
# 利用函数将str3中的数字加1
28+
def add(match):
29+
val = match.group()
30+
num = int(val) + 1
31+
return str(num)
32+
33+
info = re.sub(r'\d+', add, str3)
34+
print(info)
35+
36+
# split
37+
str4 = 'class=C C++ Java Python, C#'
38+
info = re.split(r'=| |,', str4)
39+
print(info)

regularExpression/re练习.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
抓取腾讯主页中的图片到本地
3+
1. 抓取网页
4+
2. 抓取图片地址
5+
3. 抓取图片内容并保存到本地
6+
'''
7+
8+
9+
import requests
10+
def getdata():
11+
url = 'http://www.qq.com/'
12+
buf = requests.get(url)
13+
return buf
14+
15+
buf = getdata()
16+
17+
import re
18+
from numpy import *
19+
20+
# 抓取png和jpg图像
21+
listurl1 = re.findall(r'src="http://.*\.png', buf.text)
22+
listurl2 = re.findall(r'src="http://.*\.jpg', buf.text)
23+
listURL = []
24+
25+
# 去除url上不必要的前缀
26+
for url in listurl1:
27+
listURL.append(re.findall(r'http:.*\.png', url))
28+
for url in listurl2:
29+
listURL.append(re.findall(r'http:.*\.jpg', url))
30+
31+
# 将网上图片写入本地
32+
i = 0
33+
for url in listURL:
34+
f = open(str(i)+'.png', 'wb')
35+
buf = requests.get(url[0], stream=True)
36+
for chunk in buf.iter_content(chunk_size=1024):
37+
if chunk:
38+
f.write(chunk)
39+
f.flush()
40+
f.close()
41+
i += 1
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import re
2+
3+
ma = re.match(r'a', 'a')
4+
print(ma.group())
5+
6+
# 使用 '.' 匹配任意字符
7+
ma = re.match(r'.', 'b')
8+
print(ma.group())
9+
10+
ma = re.match(r'{.}', '{a}') # 匹配大括号中的任意一个字符
11+
print(ma.group())
12+
13+
ma = re.match(r'{..}', '{ab}') # 匹配大括号中的任意两个字符
14+
print(ma.group())
15+
16+
# 使用 [...] 匹配字符集
17+
ma = re.match(r'{[abc]}', '{a}') # 匹配大括号中为a或b或c的值
18+
print(ma.group())
19+
20+
ma = re.match(r'{[a-z]}', '{d}') # 匹配a-z的任意一个字符
21+
print(ma.group())
22+
23+
ma = re.match(r'{[a-zA-Z]}', '{Z}')
24+
print(ma.group())
25+
26+
ma = re.match(r'{[\w]}', '{0}') # 使用`\w`匹配a-zA-Z0-9的任意一个字符
27+
print(ma.group())
28+
29+
# 对于中括号中的匹配, 需要加一个转意
30+
ma = re.match(r'\[[\w]\]', '[a]') # '\['和'\]'用来转意
31+
print(ma.group())
32+
33+
ma = re.match(r'[A-Z][a-z]', 'Aa')
34+
print(ma.group())
35+
36+
ma = re.match(r'[A-Z][a-z]*', 'A') # 匹配一个大写字母和0个或无穷多个小写字母
37+
print(ma.group())
38+
ma = re.match(r'[A-Z][a-z]*', 'Aa')
39+
print(ma.group())
40+
ma = re.match(r'[A-Z][a-z]*', 'Aafdsfdsb')
41+
print(ma.group())
42+
ma = re.match(r'[A-Z][a-z]*', 'Aafdsfdsbadfas154154') # 此例子只匹配数字前的字符
43+
print(ma.group())
44+
45+
# 匹配以至少一个为_或者a-z或者A-Z开头的字符, 后面接无线多个_或者a-z或者A-Z。可以用于检测变量名是否合法
46+
# 注意这里 + 代表的是匹配前一个字符1次或者无限次
47+
ma = re.match(r'[_a-zA-Z]+[_\w]*', '__init')
48+
print(ma.group())
49+
50+
ma = re.match(r'[1-9]?[0-9]', '99') # 按照正常显示0-99的匹配, 09匹配不上, 9可以匹配上。?代表匹配一次或者0次
51+
print(ma.group())
52+
53+
ma = re.match(r'[1-9]?[0-9]', '09') # 返回 0 , 不是09
54+
print(ma.group())
55+
56+
ma = re.match(r'[a-zA-Z0-9]{6}', 'bac123') # a-zA-Z0-9匹配六次
57+
print(ma.group())
58+
# ma = re.match(r'[a-zA-Z0-9]{6}', 'bac12') # 返回空, 因为bac12长度小于6
59+
# print(ma.group())
60+
ma = re.match(r'[a-zA-Z0-9]{6}', 'bac1234') # bac123
61+
print(ma.group())
62+
63+
# 长度为6的163邮箱匹配
64+
ma = re.match(r'[a-zA-Z0-9]{6}@163.com', 'abc123@163.com')
65+
print(ma.group())
66+
# 程度为6-10的163邮箱匹配
67+
ma = re.match(r'[a-zA-Z0-9]{6,10}@163.com', 'abc123gd@163.com')
68+
print(ma.group())
69+
70+
ma = re.match(r'[0-9][a-z]*?', '1bc') # 只匹配第一个1, 采取非贪婪模式, 尽可能少匹配
71+
print(ma.group())
72+
73+
ma = re.match(r'[0-9][a-z]+?', '1bc') # 1b
74+
print(ma.group())
75+
76+
# 匹配字符串的开头或者结尾
77+
ma = re.match(r'[\w]{4,10}@163.com', 'abc123gd@163.comabc') # abc123gd@163.com 但是原始字符串不是我们想要的
78+
print(ma.group())
79+
# ma = re.match(r'[\w]{4,10}@163.com$', 'abc123gd@163.comabc') # 报错, 加上$后, 结尾必须是@163.com
80+
# print(ma.group())
81+
82+
ma = re.match(r'^[\w]{4,10}@163.com$', 'abc123gd@163.com') # 前面加上 ^ , 必须以[\w]开头
83+
print(ma.group())
84+
85+
86+
ma = re.match(r'\Aabc[\w]{4,10}@163.com$', 'abc123gd@163.com') # \A 限定了开头必须以 abc 开头
87+
print(ma.group())

0 commit comments

Comments
 (0)