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
8e3cdfcb
Unverified
Commit
8e3cdfcb
authored
Nov 02, 2020
by
Kiryuu
Browse files
Finish python advanced
parent
c61d5935
Changes
10
Show whitespace changes
Inline
Side-by-side
01. Python regex/02. python advanced/README.md
0 → 100644
View file @
8e3cdfcb
# Python正则表达式进阶
> 正则表达式在实际应用中,可以匹配非常复杂的字符串,但是需要使用到它更加高级的用法。
>
> 本实训主要介绍:
> - 正则表达式分组
> - 正则表达式断言
> - 正则表达式标记
> - 正则表达式编译
https://www.educoder.net/shixuns/6538mi2v/challenges
## 第1关:分组
### 任务描述
本关任务:使用分组完成对中国手机号码的提取
### 相关知识
#### 分组
要实现分组很简单,使用()即可。从正则表达式的左边开始看,看到的第一个左括号(表示表示第一个分组,第二个表示第二个分组,依次类推。
```
python
a
=
'<div><a href="https://support.google.com/chrome/?p=ui_hotword_search" target="_blank">python正则表达式之分组</a><p>dfsl</p></div>'
print
(
re
.
search
(
r
'<a.*>(.*)</a>'
,
a
).
group
(
1
))
```
输出:
`python正则表达式之分组`
*需要注意的是,有一个隐含的全局分组(就是索引号为0的分组),就是整个正则表达式匹配的结果。*
#### 命名分组
命名分组就是给具体有默认分组编号的组另外再起一个别名,方便以后的引用。
命令分组的语法格式如下:
`(?P<name>正则表达式)`
语法格式中的字符
`P`
必须是大写的
`P`
,
`name`
是一个合法的标识符,表示分组的别名。如下例子:
```
python
a
=
"ip='127.0.0.1',version='1.0.0'"
res
=
re
.
search
(
r
"ip='(?P<ip>\d+\.\d+\.\d+\.\d+).*"
,
a
)
print
(
res
.
group
(
'ip'
))
#通过命名分组引用分组
```
输出:
`127.0.0.1`
### 编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体任务如下:
-
提取11位数字的手机号码,过滤掉字符串中其他符号。
### 测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
输入:
```
(86)-17712576838,86 14295083635,(+86) 13722348123,17587918887,-15493106739,.13786842977,86-15542304386,+86.15642387356,17345352790
```
经过 Python 文件 stepm.py 处理为以下格式:
```
python
"""
(86)-17712576838
86 14295083635
(+86) 13722348123
17587918887
-15493106739
.13786842977
86-15542304386
+86.15642387356
17345352790"""
```
你将使用以上格式的数据完成任务!
预期输出:
```
python
[
'17712576838'
,
'14295083635'
,
'13722348123'
,
'17587918887'
,
'15493106739'
,
'13786842977'
,
'15542304386'
,
'15642387356'
,
'17345352790'
]
```
---
开始你的任务吧,祝你成功!
### 答案
```
python
import
re
def
re_group
(
input_data
):
return
[
span
.
group
(
0
)
for
span
in
re
.
finditer
(
r
'1\d{10}'
,
input_data
)]
```
## 第2关:先行断言
### 任务描述
本关任务:编写正则表达式,使用正向先行断言获取字符串中以ing结尾的字符(不能为空)。
### 相关知识
先行断言分为正向先行断言和反向先行断言,完成本关任务需要了解这两个知识点。
#### 正向先行断言
`(?=pattern)`
表示正向先行断言,整个括号里的内容(包括括号本身)代表字符串中的一个位置,紧接该位置之后的字符序列能够匹配pattern。举个例子:
```
python
# `(?!e)`代表字符串中的一个位置,紧接该位置之后的字符序列只能够匹配`e`。
a
=
re
.
findall
(
r
'n(?=al)'
,
'final'
)
b
=
re
.
findall
(
r
'n(?=e)'
,
'python'
)
c
=
re
.
findall
(
r
'n(?=e)'
,
'jasmine'
)
print
(
a
)
print
(
b
)
print
(
c
)
```
输出:
```
python
[
'n'
]
[]
[
'n'
]
```
#### 反向先行断言
`(?!pattern)`
表示反向先行断言,与正向先行断言相反,紧接该位置之后的字符序列不能够匹配
`pattern`
。同样举个例子:
```
python
a
=
re
.
findall
(
r
'n(?!e)'
,
'final'
)
b
=
re
.
findall
(
r
'n(?!e)'
,
'python'
)
c
=
re
.
findall
(
r
'n(?!e)'
,
'next'
)
print
(
a
)
print
(
b
)
print
(
c
)
```
输出:
```
python
[
'n'
]
[
'n'
]
[]
```
**注意:反向断言不支持匹配不定长的表达式,也就是说 `+`、`*` 字符不适用于反向断言的前后**
### 编程要求
请根据相关知识补充右侧Begin-End之间的代码,并完成下列任务:
-
使用正向先行断言获取字符串中以ing结尾的字符(不能为空)。
### 测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
测试输入:
`I am singing while you are dancing`
;
预期输出:
`['sing', 'danc']`
。
----
开始你的任务吧,祝你成功!
### 答案
```
python
import
re
a
=
input
()
#*********** Begin **********#
result
=
re
.
findall
(
r
'\S+(?<=\S)(?=ing)(?!inging)'
,
a
)
#*********** End **********#
print
(
result
)
```
## 第3关:后发断言
### 任务描述
本关任务:编写正则表达式,使用正向后发断言获取字符串中以 go 开头的字符的后半部分。
### 相关知识
后行断言分为
**正向后发断言**
和
**反向后发断言**
,完成本关需要掌握这两个知识点。
#### 正向后发断言
`(?<=pattern)`
正向后发断言代表字符串中的一个位置,紧接该位置之前的字符序列只能够匹配
`pattern`
。
```
python
a
=
re
.
findall
(
'(?<=a)n'
,
'final'
)
b
=
re
.
findall
(
'(?<=a)n'
,
'command'
)
c
=
re
.
findall
(
'(?<=i)n'
,
'negative'
)
print
(
a
)
print
(
b
)
print
(
c
)
```
输出:
```
python
[]
[
'n'
]
[]
```
#### 反向后发断言
`(?<!pattern)`
负向后发断言 代表字符串中的一个位置,紧接该位置之前的字符序列不能匹配
`pattern`
。
```
python
a
=
re
.
findall
(
'(?<!i)n'
,
'final'
)
b
=
re
.
findall
(
'(?<!a)n'
,
'command'
)
c
=
re
.
findall
(
'(?<!i)n'
,
'negative'
)
print
(
a
)
print
(
b
)
print
(
c
)
```
输出:
```
python
[]
[]
[
'n'
]
```
*从上面的描述可以看出,先行和后发的区别就是是否有 `<`,正向与反向的区别是 `=` 和 `!`。是不是非常容易记呢?*
### 编程要求
请根据相关知识补充右侧Begin-End之间的代码,并完成下列任务:
-
使用正向后发断言匹配字符串中以go开头的字符的后半部分。
### 测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
测试输入:
`good job`
;
预期输出:
`['od']`
。
开始你的任务吧,祝你成功!
### 答案
```
python
import
re
a
=
input
()
#*********** Begin **********#
result
=
re
.
findall
(
r
'(?<=go).+?\b'
,
a
)
#*********** End **********#
print
(
result
)
```
\ No newline at end of file
01. Python regex/02. python advanced/step1/step1.py
0 → 100644
View file @
8e3cdfcb
import
re
def
re_group
(
input_data
):
result
=
[]
#*********** Begin **********#
#*********** End **********#
return
result
01. Python regex/02. python advanced/step1/stepm.py
0 → 100644
View file @
8e3cdfcb
import
step1
if
__name__
==
'__main__'
:
str
=
""
a
=
input
()
for
i
in
a
.
split
(
","
):
str
+=
"
\n
"
str
+=
i
print
(
step1
.
re_group
(
str
))
\ No newline at end of file
01. Python regex/02. python advanced/step2/demo.py
0 → 100644
View file @
8e3cdfcb
import
re
a
=
input
()
#*********** Begin **********#
#*********** End **********#
print
(
result
)
01. Python regex/02. python advanced/step2/demo1.py
0 → 100644
View file @
8e3cdfcb
import
re
a
=
input
()
#*********** Begin **********#
#*********** End **********#
print
(
result
)
\ No newline at end of file
01. Python regex/02. python advanced/step3/step1.py
0 → 100644
View file @
8e3cdfcb
import
re
def
re_mark
(
input_data
):
result
=
[]
#*********** Begin **********#
result
=
#*********** End **********#
return
result
01. Python regex/02. python advanced/step3/stepm.py
0 → 100644
View file @
8e3cdfcb
import
step1
if
__name__
==
'__main__'
:
a
=
input
()
str
=
""
for
i
in
a
.
split
(
"
\\
n"
):
str
+=
i
str
+=
"
\n
"
print
(
step1
.
re_mark
(
str
))
\ No newline at end of file
01. Python regex/02. python advanced/step4/Main.py
0 → 100644
View file @
8e3cdfcb
import
Test
if
__name__
==
'__main__'
:
str
=
input
()
print
(
Test
.
re_telephone
(
str
))
01. Python regex/02. python advanced/step4/Test.py
0 → 100644
View file @
8e3cdfcb
import
re
def
re_telephone
(
str
):
#*********** Begin **********#
#*********** End **********#
return
re_group
01. Python regex/02. python advanced/step4/__init__.py
0 → 100644
View file @
8e3cdfcb
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