-
Notifications
You must be signed in to change notification settings - Fork 0
/
re_test.py
68 lines (54 loc) · 1.24 KB
/
re_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'''
https://zhuanlan.zhihu.com/p/55916454
'''
import re
content = 'Xiaoshuaib has 100 bananas'
res = re.match('^Xi.*(\d+)\s.*s$',content)
print(res.group(1))
'''
import re
content = """Xiaoshuaib has 100
bananas"""
res = re.match('^Xi.*?(\d+)\s.*s$',content,re.S)
print(res.group(1))
'''
'''
import re
content = """Xiaoshuaib has 100
bananas"""
res = re.search('Xi.*?(\d+)\s.*s',content,re.S)
print(res.group(1))
'''
'''
import re
content = """Xiaoshuaib has 100 bananas;
Xiaoshuaib has 100 bananas;
Xiaoshuaib has 100 bananas;
Xiaoshuaib has 100 bananas;"""
res = re.findall('Xi.*?(\d+)\s.*?s;',content,re.S)
print(res)
# ['100', '100', '100', '100']
'''
'''
import re
content = """Xiaoshuaib has 100 bananas;
Xiaoshuaib has 100 bananas;
Xiaoshuaib has 100 bananas;
Xiaoshuaib has 100 bananas;"""
content = re.sub('\d+','250',content)
print(content)
#Xiaoshuaib has 250 bananas;
#Xiaoshuaib has 250 bananas;
#Xiaoshuaib has 250 bananas;
#Xiaoshuaib has 250 bananas;
'''
'''
import re
content = "Xiaoshuaib has 100 bananas"
pattern = re.compile('Xi.*?(\d+)\s.*s',re.S)
res = re.match(pattern,content)
print(res.group(1))
#其实和我们之前写的一样的
#res = re.match('^Xi.*?(\d+)\s.*s$',content,re.S)
#只不过 compile 一下 便于以后复用
'''