forked from techstay/python-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.py
More file actions
56 lines (39 loc) · 1.13 KB
/
regex.py
File metadata and controls
56 lines (39 loc) · 1.13 KB
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
import re
print(f'{"-"*20}模式对象{"-"*20}')
text = 'fuck shit it make she forest'
results = re.findall(r'\b[Ff]\w+', text)
print(results)
pattern = re.compile(r'\b[Ff]\w+')
print(pattern.findall(text))
print(pattern.pattern)
print(pattern.groups)
print(pattern.flags)
print(f'{"-"*20}search{"-"*20}')
print(pattern.search(text))
print(f'{"-"*20}match{"-"*20}')
result = pattern.match(text)
print(result)
print(f'{"-"*20}fullmatch{"-"*20}')
result = pattern.fullmatch(text)
print(result)
result = pattern.fullmatch('fuck')
print(result)
print(f'{"-"*20}split{"-"*20}')
print(pattern.split(text))
print(f'{"-"*20}findall{"-"*20}')
print(pattern.findall(text))
print(f'{"-"*20}finditer{"-"*20}')
print(pattern.finditer(text))
print(f'{"-"*20}sub{"-"*20}')
result = pattern.sub("fffffuck", text)
print(result)
print(f'{"-"*20}subn{"-"*20}')
t = pattern.subn('ffffuck', text)
print(t)
print(f'{"-"*20}匹配对象{"-"*20}')
text = '总共20条数据 每页5条'
pattern = re.compile(r'总共(?P<total>\d+)条数据\s+每页(?P<per>\d+)条')
match = pattern.match(text)
print(match.group(1))
print(match.groups())
print(match.groupdict())