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
c61d5935
Unverified
Commit
c61d5935
authored
Nov 02, 2020
by
Kiryuu
Browse files
Finish python regex test & regex tutorial
parent
35afeaed
Changes
2
Hide whitespace changes
Inline
Side-by-side
01. Python regex/01. python regex test/README.md
View file @
c61d5935
# python正则表达式综合练习
\ No newline at end of file
# python正则表达式综合练习
> 本实训旨在检验你是否掌握正则常用方法的使用,赶紧来挑战吧!
https://www.educoder.net/shixuns/pbvjhm8z/challenges
## 第1关:提取日志内容
### 任务描述
本关任务:使用Python正则提取日志内容。
### 编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体任务如下:
-
编写正则表达式提取日志中
`cs_item_sk`
的数值以1结尾的并且布尔值为 true 的日志内容,然后打印匹配到的日志内容。其中文件输入路径为:
`./test_one/src.txt`
。
文件中的部分内容如下所示:
```
[main] INFO com.jzdata.press.core.PressTest - select
cs_bill_customer_sk,count(*) from catalog_sales where cs_item_sk =13
group by cs_bill_customer_sk order by cs_bill_customer_sk limit 100;
true 291
[main] INFO com.jzdata.press.core.PressTest - select
cs_bill_customer_sk,count(*) from catalog_sales where cs_item_sk =111
group by cs_bill_customer_sk order by cs_bill_customer_sk limit 100;
true 320
[main] INFO com.jzdata.press.core.PressTest - select
cs_bill_customer_sk,count(*) from catalog_sales where cs_item_sk =312
group by cs_bill_customer_sk order by cs_bill_customer_sk limit 100;
true 320
```
以上内容匹配到的日志内容为
`('111', '320')`
。
### 测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
预期输出:
`('111', '320')`
----
开始你的任务吧,祝你成功!
### 答案
```
python
import
re
def
re_Regex
():
#*********** Begin **********#
# 读取数据文件
pattern
=
re
.
compile
(
r
'^\[.+cs_item_sk\s*=(\d*1)\D.+?true\s*(\d+)'
,
re
.
DOTALL
)
with
open
(
'./test_one/src.txt'
)
as
f
:
for
span
in
pattern
.
findall
(
''
.
join
(
f
.
readlines
())):
print
(
span
)
# 根据日志数据编写正则表达式提取数据内容
# 提取cs_item_sk的数值以1结尾的并且布尔值为true的所需的日志内容
#*********** End **********#
```
## 第2关:组合密码匹配
### 任务描述
本关任务:编写正则获取一组密码中的正确密码。
### 编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,正确密码的具体要求如下:
-
正确密码包括数字,字母,特殊字符;
-
包含空格,换行,制表符等空字符的密码无效;
-
密码不能为纯数字,纯字母,纯特殊字符。
### 测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
测试输入:
```
python
abc123
@
.,
good123
...,
666
educoder
^
,
hello123
@
,
123456789
```
预期输出:
```
python
[
'abc123@.'
,
'666educoder^'
,
'hello123@'
]
```
注意在文件
`stepm2.py`
中,测试数据被处理为以下格式:
```
python
"""
abc123@.
good123...
666educoder^
hello123@
123456789
"""
```
----
开始你的任务吧,祝你成功!
### 答案
```
python
import
re
def
re_test2
(
input_data
):
result
=
[]
#*********** Begin **********#
result
=
[]
pattern1
=
re
.
compile
(
r
"^\S+$"
)
pattern2
=
re
.
compile
(
r
"^\d+$"
)
pattern3
=
re
.
compile
(
r
"^\w+$"
)
pattern4
=
re
.
compile
(
r
"^\W+$"
)
for
line
in
input_data
.
split
(
'
\n
'
):
if
pattern1
.
match
(
line
)
is
not
None
\
and
pattern2
.
match
(
line
)
is
None
\
and
pattern3
.
match
(
line
)
is
None
\
and
pattern4
.
match
(
line
)
is
None
:
result
.
append
(
line
)
return
result
#*********** End **********#
return
result
```
## 第3关:网页内容解析
### 任务描述
本关任务:使用python正则解析网页内容。
### 编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体说明如下:
1.
`parse_one_page()`
函数传入参数 html 为网页源代码;
2.
根据网页源代码提取电影信息,并打印匹配到的结果。获取内容格式如下:
`board-index ***`
(排名)、
`date-src`
(图片地址)、
`title`
(影片名)、
`star`
(主演)、
`releasetime`
(上映时间)、
`integer`
、
`fraction`
(评分 如:
`9.5`
`integer:9.`
`fraction:5`
)标签组合。
参考 html 文本:
```
html
<dd>
<i
class=
"board-index board-index-1"
>
1
</i>
<a
href=
"/films/1203"
title=
"霸王别姬"
class=
"image-link"
data-act=
"boarditem-click"
data-val=
"{movieId:1203}"
>
<img
src=
"//s3plus.meituan.net/v1/mss_e2821d7f0cfe4ac1bf9202ecf9590e67/cdn-prod/file:5788b470/image/loading_2.e3d934bf.png"
alt=
""
class=
"poster-default"
/>
<img
data-src=
"https://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c"
alt=
"霸王别姬"
class=
"board-img"
/>
</a>
<div
class=
"board-item-main"
>
<div
class=
"board-item-content"
>
<div
class=
"movie-item-info"
>
<p
class=
"name"
><a
href=
"/films/1203"
title=
"霸王别姬"
data-act=
"boarditem-click"
data-val=
"{movieId:1203}"
>
霸王别姬
</a></p>
<p
class=
"star"
>
主演:张国荣,张丰毅,巩俐
</p>
<p
class=
"releasetime"
>
上映时间:1993-01-01
</p>
</div>
<div
class=
"movie-item-number score-num"
>
<p
class=
"score"
><i
class=
"integer"
>
9.
</i><i
class=
"fraction"
>
5
</i></p>
</div>
</div>
</div>
</dd>
```
### 测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
测试输入:
`./test_three/霸王别姬.html`
预期输出:
```
python
[(
'1'
,
'https://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c'
,
'霸王别姬'
,
'主演:张国荣,张丰毅,巩俐'
,
'上映时间:1993-01-01'
,
'9.'
,
'5'
)]
```
----
开始你的任务吧,祝你成功!
### 答案
> 我吐了
```
python
import
re
def
parse_one_page
(
string
):
#*********** Begin **********#
result
=
[]
result
+=
re
.
findall
(
r
'<i class="board-index.*?">([^<]+)'
,
string
),
result
+=
re
.
findall
(
r
'<img data-src="([^"]+)'
,
string
),
result
+=
re
.
findall
(
r
'<a .*?title="([^"]+)'
,
string
),
result
+=
re
.
findall
(
r
'<p class="star">\s*([^<\s]+)'
,
string
),
result
+=
re
.
findall
(
r
'<p class="releasetime">\s*([^<\s]+)'
,
string
),
result
+=
re
.
findall
(
r
'<i class="integer">\s*([^<\s]+)'
,
string
),
result
+=
re
.
findall
(
r
'<i class="fraction">\s*([^<\s]+)'
,
string
),
print
(
list
(
map
(
tuple
,
zip
(
*
result
))))
#*********** End **********#
```
\ No newline at end of file
01. Python regex/03. regex tutorial/README.md
View file @
c61d5935
...
...
@@ -17,7 +17,7 @@ https://www.educoder.net/shixuns/3rahy6ck/challenges
1.
如何在 python 中引入正则表达式库;
2.
re库中search方法的使用。
#### 1、在Python 中使用正则表达式
正可谓人生苦短,我用Python。Python有个特点就是库非常多,自然拥有正则匹配这种常见的库,并且此库已经嵌入在Python标准库中,使用起来非常方便,只需要在代码中导入re模块即可。
...
...
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