forked from liuyuzhou/python3.7sourcecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_kinds_match.py
More file actions
30 lines (25 loc) · 856 Bytes
/
Copy pathall_kinds_match.py
File metadata and controls
30 lines (25 loc) · 856 Bytes
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
# -*- coding:UTF-8 -*-
import re
# 匹配目标
def target_match(content):
result = re.match('^Hello\s(\d+)\sWorld', content)
return result, result.group(), result.group(1), result.span()
# 通用匹配
def gena_match(content):
result = re.match('^Hello.*Demo$', content)
return result, result.group(), result.span()
# 贪婪匹配
def greed_match(content):
result = re.match('^He.*(\d+).*Demo$', content)
return result, result.group(1)
# 非贪婪匹配
def un_greed_match(content):
result = re.match('^He.*(\d+).*Demo$', content)
return result, result.group(1)
if __name__ == "__main__":
con_match = 'Hello 1234567 World_This is a Regex Demo'
target_match(con_match)
gena_match(con_match)
greed_match(con_match)
result_v, result_group = un_greed_match(con_match)
print(result_v, result_group)