Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
python3-cookbook/basic/mystring/re_search.py at master · pythoningLearning/python3-cookbook · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
pythoningLearning
/
python3-cookbook
Public
forked from
yidao620c/python3-cookbook
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
python3-cookbook
/
basic
/
mystring
/
re_search.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
90 lines (76 loc) · 3.38 KB
master
Breadcrumbs
python3-cookbook
/
basic
/
mystring
/
re_search.py
Copy path
Top
File metadata and controls
Code
Blame
90 lines (76 loc) · 3.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 简单的正则式搜索,包括分组捕获
Desc : 正则式例子
建议阅读Jeffrey E. F. Friedl编写的《精通正则表达式》
(Mastering Regular Expression)
"""
import
re
__author__
=
'Xiong Neng'
def
my_re
():
# group示例
data
=
'Thu'
patt1
=
r'^(\w{3})'
m
=
re
.
match
(
patt1
,
data
)
print
(
m
.
group
(
1
))
patt2
=
r'^(\w){3}'
m
=
re
.
match
(
patt2
,
data
)
print
(
m
.
group
(
1
))
# 贪婪匹配
data
=
"Sat Mar 21 09:20:57 2009::spiepu@ovwdmrnuw.com::1237598457-6-9"
# 获取最后的那三个连字符连起来的三个数,
# 搜索比匹配更合适,因为不在开头
patt
=
r'\d+-\d+-\d+'
print
(
re
.
search
(
patt
,
data
).
group
())
# 打印出 1237598457-6-9
# 使用匹配,必须用到group
patt
=
r'.+(\d+-\d+-\d+)'
print
(
re
.
match
(
patt
,
data
).
group
(
1
))
# 打印出 7-6-9,知道贪婪的厉害了吧。哈哈
# 接下来使用非贪婪操作符?
patt
=
r'.+?(\d+-\d+-\d+)'
print
(
re
.
match
(
patt
,
data
).
group
(
1
))
# 打印出 1237598457-6-9
# 只获取三个数的中间那个数字:
patt
=
r'-(\d+)-'
print
(
re
.
search
(
patt
,
data
).
group
())
# 打印-6-
print
(
re
.
search
(
patt
,
data
).
group
(
1
))
# 打印6
def
my_pattern
():
# (?ims) i表示忽略大小写,m表示多行模式,s表示.可以匹配所有字符,包括换行符
s
=
"This is a book, And Hello World!! Hello you World?"
print
(
1
,
re
.
match
(
r'.*Hello'
,
s
).
group
())
# 匹配圆括号中的正则式,但丢弃匹配的子字符串
print
(
2
,
re
.
match
(
r'.*(?:Hello)'
,
s
).
group
())
# 分组名为haha
print
(
3
,
re
.
match
(
r'.*(?P<haha>Hello)'
,
s
).
group
())
# 注释,括号中内容被忽略
print
(
4
,
re
.
match
(
r'.*(?#Hello)'
,
s
).
group
())
# 只有在括号中的模式匹配时,才匹配前面的表达式
print
(
5
,
re
.
match
(
r'.*Hello (?=World)'
,
s
).
group
())
# 只有在括号中的模式不匹配时,才匹配前面的表达式
print
(
6
,
re
.
match
(
r'.*Hello (?!World)'
,
s
).
group
())
# 只有在括号中的模式匹配时,才匹配后面的表达式
print
(
7
,
re
.
match
(
r'.*(?<=Hello )World'
,
s
).
group
())
# 只有在括号中的模式不匹配时,才匹配后面的表达式
print
(
8
,
re
.
match
(
r'.*(?<!Hello )World'
,
s
).
group
())
# (?(id|name)ypat|npat) 检查id或name组是否存在,如果存在匹配ypat,否则npat
print
(
9
,
re
.
match
(
r'.*(Hello )?(?(1)World|Howdy)'
,
s
).
group
())
print
(
9.1
,
re
.
search
(
r'(Hello )?(?(1)World|Howdy)'
,
s
).
group
())
def
my_sample
():
"""一个详细的字符串搜索、提取、替换示例"""
text
=
"Guido will be out of the office from 12/15/2012 - 1/3/2013"
# 日期的正则式
datepat
=
re
.
compile
(
r'(\d+)/(\d+)/(\d+)'
)
# 找到并打印所有日期
for
m
in
datepat
.
finditer
(
text
):
print
(
m
.
group
())
# z找到所有日期,但是以不同格式打印
monthnames
=
[
None
,
'Jan'
,
'Feb'
,
'Mar'
,
'Apr'
,
'May'
,
'Jun'
,
'Jul'
,
'Aug'
,
'Sep'
,
'Oct'
,
'Nov'
,
'Dec'
]
for
m
in
datepat
.
finditer
(
text
):
print
(
"%s %s, %s"
%
(
monthnames
[
int
(
m
.
group
(
1
))],
m
.
group
(
2
),
m
.
group
(
3
)))
# 将所有日期替换为欧洲日期格式(日/月/年)
def
fix_date
(
m
):
return
"%s/%s/%s"
%
(
m
.
group
(
2
),
m
.
group
(
1
),
m
.
group
(
3
))
newtext
=
datepat
.
sub
(
fix_date
,
text
)
print
(
newtext
)
if
__name__
==
'__main__'
:
my_sample
()
You can’t perform that action at this time.