Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Kiryuu Sakuya
data-analysis
Commits
fc8e62b5
Unverified
Commit
fc8e62b5
authored
Nov 02, 2020
by
Kiryuu
Browse files
Finish python advanced
parent
8e3cdfcb
Changes
1
Hide whitespace changes
Inline
Side-by-side
01. Python regex/02. python advanced/README.md
View file @
fc8e62b5
...
@@ -269,4 +269,268 @@ result = re.findall(r'(?<=go).+?\b', a)
...
@@ -269,4 +269,268 @@ result = re.findall(r'(?<=go).+?\b', a)
#*********** End **********#
#*********** End **********#
print
(
result
)
print
(
result
)
```
## 第4关:标记
### 任务描述
本关任务:编写正则匹配出以study开头的邮箱,不区分大小写。
### 相关知识
为了完成本关任务,你需要掌握:
1.
Python正则表达式基本用法;
2.
Python正则表达式标记。
#### 不区分大小写
`re.IGNORECASE`
也可以简写为
`re.I`
,使用该标记,可以使正则表达式变为不区分大小写。
示例:
```
python
a
=
re
.
search
(
r
'apple'
,
"THIS IS AN APPLE"
,
re
.
IGNORECASE
)
b
=
re
.
search
(
r
'apple'
,
'THIS IS AN APPLE'
,
re
.
I
)
print
(
a
)
print
(
b
)
```
输出:
```
python
<
_sre
.
SRE_Match
object
;
span
=
(
11
,
16
),
match
=
'APPLE'
>
<
_sre
.
SRE_Match
object
;
span
=
(
11
,
16
),
match
=
'APPLE'
>
```
#### 点匹配换行符
`re.DOTALL`
标记(别名为
`re.S`
)可以让
`.`
字符除了匹配其他字符之外,还匹配换行符。
示例:
```
python
a
=
re
.
search
(
r
'.+'
,
'hello
\n
python'
)
b
=
re
.
search
(
r
'.+'
,
'hello
\n
python'
,
re
.
S
)
c
=
re
.
search
(
r
'.+'
,
'hello
\n
python'
,
re
.
DOTALL
)
print
(
a
)
print
(
b
)
print
(
c
)
```
输出:
```
python
<
_sre
.
SRE_Match
object
;
span
=
(
0
,
5
),
match
=
'hello'
>
<
_sre
.
SRE_Match
object
;
span
=
(
0
,
12
),
match
=
'hello
\n
python'
>
<
_sre
.
SRE_Match
object
;
span
=
(
0
,
12
),
match
=
'hello
\n
python'
>
```
#### 多行模式
`re.MULTILINE`
标记(别名为
`re.M`
)可以匹配多行,使用该标记可以使得仅能够匹配字符串开始与结束的
`^`
与
`$`
字符可以匹配字符串内任意行的开始与结束。
```
python
a
=
re
.
search
(
r
'^like'
,
'foo
\n
like'
)
b
=
re
.
search
(
r
'^like'
,
'foo
\n
like'
,
re
.
M
)
print
(
a
)
print
(
b
)
```
输出:
```
python
None
<
_sre
.
SRE_Match
object
;
span
=
(
4
,
8
),
match
=
'like'
>
```
#### 详细模式
`re.VERBOSE`
标记(别名为
`re.X`
)允许复杂的正则表达式以更容易的方式表示。
该标记做两件事,首先,它会使所有的空白(除了字符组中)被忽略,包括换行符。其次,它将
`#`
字符(同样,除非在字符组内)当做注释字符。
示例:
```
python
a
=
re
.
search
(
r
'(?P<first>[\d]{3})-(?P<second>[\d]{4})'
,
'867-5556'
)
b
=
re
.
search
(
r
"""(?P<first>[\d]{3})
- #匹配一个 - 连接符
(?P<second>[\d]{4}) # 匹配四个数字
"""
,
'010-1234'
,
re
.
X
)
print
(
a
)
print
(
b
)
```
输出:
```
python
<
_sre
.
SRE_Match
object
;
span
=
(
0
,
8
),
match
=
'867-5556'
>
<
_sre
.
SRE_Match
object
;
span
=
(
0
,
8
),
match
=
'010-1234'
>
```
#### 调试模式
`re.DEBUG`
标记(没有别名)在编译正则表达式时将一些调试信息输出到
`sys.stderr`
示例:
```
python
a
=
re
.
search
(
r
'(?P<first>[\d]{3})-(?P<second>[\d]{4})'
,
'010-1234'
,
re
.
DEBUG
)
print
(
a
)
```
输出:
```
python
SUBPATTERN
1
MAX_REPEAT
3
3
IN
CATEGORY
CATEGORY_DIGIT
LITERAL
45
SUBPATTERN
2
MAX_REPEAT
4
4
IN
CATEGORY
CATEGORY_DIGIT
<
_sre
.
SRE_Match
object
;
span
=
(
0
,
8
),
match
=
'010-1234'
>
```
#### 使用多个标记
有时候我们可能需要同时使用多个标记,为了完成这点,可以使用
`|`
操作符。
示例:
`re.DOTALL|re.MULTILINE`
或
`re.S | re.M`
。
### 编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体任务如下:
-
匹配出以study开头的邮箱,不区分大小写。
### 测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
### 测试输入:
```
python
'Study123456@qq.com
\n
sTudyleslie@163.com
\n
STUDY@gmail.com
\n
astudyeasy@163.com
\n
stuDYgood@gmail.com'
```
输入经过 stepm.py 文件处理为以下格式:
```
python
"""
Study123456@qq.com
sTudyleslie@163.com
STUDY@gmail.com
astudyeasy@163.com
stuDYgood@gmail.com
"""
```
你将使用以上格式的数据完成任务!
预期输出:
```
python
[
'Study123456@qq.com'
,
'sTudyleslie@163.com'
,
'STUDY@gmail.com'
,
'stuDYgood@gmail.com'
]
```
----
开始你的任务吧,祝你成功!
### 答案
```
python
import
re
def
re_mark
(
input_data
):
return
[
span
.
group
(
0
)
for
span
in
re
.
finditer
(
r
'^study.*@.+\..+'
,
input_data
,
re
.
MULTILINE
|
re
.
IGNORECASE
)]
```
## 第5关:编译
### 任务描述
我们已经学习了很多正则表达式相关知识,那么正则表达式编译是什么呢?本关任务就是:
1.
使用 compile 编译正则表达式;
2.
用编译后的正则表达式去匹配字符串中的数字。
### 相关知识
#### 正则表达式编译
`compile`
函数用于编译正则表达式,返回一个正则表达式对象,供
`match()`
、
`search()`
、
`findall()`
等函数使用。
示例如下:
```
python
str
=
'1Ab2Cdef3ds5ds548s4ds848we8rt6g46d46df48t6ds6x48g6s'
pattern
=
re
.
compile
(
'\D'
)
# 编译正则表达式
pattern_Math
=
pattern
.
match
(
str
)
# 决定RE是否在字符串刚开始的位置匹配。如果满足,则返回一个match对象;如果不满足,返回空。
print
(
pattern_Math
)
# 返回被RE匹配的字符串
```
输出:
`None`
使用
`re`
的一般步骤是先将正则表达式的字符串形式编译为
`pattern`
实例,然后使用
`pattern`
实例处理文本并获取匹配结果(一个
`Match`
实例(值为
`True`
)),最后使用
`Match`
实例获取信息,进行其他的操作。可以把那些经常使用的正则表达式编译成正则表达式对象,可以提高程序的执行速度。
```
python
math_Group
=
pattern
.
match
(
str
,
1
,
10
)
# 查找从索引 1 开始 10 结束
print
(
math_Group
.
group
())
# 返回被RE匹配的字符串
```
输出:
`A`
```
python
pattern_Find1
=
pattern
.
findall
(
str
)
# 找到RE匹配的所有子串,并把它们作为一个列表返回
print
(
pattern_Find1
)
# 返回一个列表
```
输出
`['A', 'b', 'C', 'd', 'e', 'f', 'd', 's', 'd', 's', 's', 'd', 's', 'w', 'e', 'r', 't', 'g', 'd', 'd', 'f', 't', 'd', 's', 'x', 'g', 's']`
```
python
pattern_Find2
=
pattern
.
findall
(
str
,
5
,
10
)
# 查找从索引 5 开始到 10 结束的所有子串,并把它们作为一个列表返回
print
(
pattern_Find2
)
```
输出:
`['d', 'e', 'f', 'd']`
### 编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体任务如下:
-
使用
`compile`
编译正则表达式;
-
用编译后的正则表达式去匹配字符串中的数字。
### 测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
测试输入:
`010-12345`
预期输出:
`('010', '12345')`
----
开始你的任务吧,祝你成功!
### 答案
```
python
import
re
def
re_telephone
(
string
):
#*********** Begin **********#
pattern
=
re
.
compile
(
r
'\d+'
)
#*********** End **********#
return
tuple
(
pattern
.
findall
(
string
))
```
```
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment