【卷一】正则四 |> 练习

时间:2023-11-19 13:33:50

参考:《Python核心编程(3rd)》—P39

1-1  识别后续的字符串: "bat", "bit", "but" "hat", "hit" 或者 "hut"

 # coding: utf-8

 # 导入re模块, re: Regex(Regular Expression) 正则表达式
import re url = "https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=bat%E7%"
# 直接用管道符号 "|" 逐一匹配
print re.findall(r"bat|bit|but|com|hat|hit|hut", url)

1-2 匹配由单个空格分隔的任意单词对,也就是姓和名

 # coding: utf-8

 import re

 txt = "My name is D_R Adams, her name is J.K Rowling, his name is Tomas Smith!"
# 管道符号左边是匹配D_R ~和J.K ~ 这种类型的,右边是匹配Tomas Smith这种常见的名字
print re.findall(r"[A-Z].[A-Z] \w+|[A-Z]\w+ [A-Z]\w+", txt)

1-6 匹配以"www"起始且以".com"结尾的简单Web域名,选做题:

你的正则表达式也可以支持其他高级域名, 如.edu, .net 等

 # coding: utf-8

 import re

 url = "http://www.baidu.com/, http://www.google.cn/, http://www.foothill.edu"

 # 有多重括号时,如果只需最外面总的分组(一般用括号把我们要的分组给圈起来),那么里面的括号就要加 ?: 表示不保存该分组
print re.findall("(www(?:.+com|.+cn|.+edu))", url)
#print re.findall("www.+com|www.+cn|www.+edu", url)

1-11 匹配所有能够表示有效电子邮件地址的集合

 # coding: utf-8

 import re

 email = "QQ mailbox such as 1111@qq.com, 1234@163.com is WangYi mailbox, and google mailbox: 2222@gmail.com"

 for each_line in email.split(","):
a = re.findall(r"\d+@.+com", each_line)
# 用 join() 把列表转换成字符串
print "".join(a)

如果只是想把文本和邮箱分开:

 # coding: utf-8
import re
email = "QQ mailbox such as 1111@qq.com, 1234@163.com is WangYi mailbox, and google mailbox: 2222@gmail.com" for each_line in email.split(","):
# 用(?= )表示按后面是 "数字+@"的情况来划分, 注意括号后边要加空格
re.split(r"(?= \d+@) ", each_line.strip())

点我

1-13 type(). 内置函数type()返回一个类型对象,创建一个从<type 'int'>提取 int, 从<type 'str'>提取str的正则表达式
 # coding: utf-8

 import re

 string = type("Hello, world!")     # str: (string)字符串
integer = type(123) # int: (integer)整数
f = type(3.14) # float: 浮点数 def PiPei(a):
# 注意,因为string, integer, f本身是type类的字符, 而不是str类的, 所以此外要转换
print re.findall(r"type '(\w+)'", str(a)) PiPei(string)
PiPei(integer)
PiPei(f)

展开

------------------------------------------------------------------------------------------------------------------------------

19-27答案参见:

正则三 之数据生成器 —> http://www.cnblogs.com/Ruby517/p/5802984.html

------------------------------------------------------------------------------------------------------------------------------

1-28 区号(三个整数集合中的第一部分和后面的连字符)是可选的,也就是说,正则表达式应当匹配 800-555-1212,也能匹配

555-1212!

 # coding: utf-8

 import re

 num1 = "555-1212"
num2 = "800-555-1212" # +表示匹配前面的字符 1 到多次,(?: )表示不保存该分组,由于我们要的是
# 一整个正则表达式匹配的内容,所以加括号的分组是不需要保存的!
print re.findall(r"(?:\d{3}-)+\d{4}", num1)
print re.findall(r"(?:\d{3}-)+\d{4}", num2)

代码

1-29 支持使用圆括号或者连字符连接的区号(更不用说是可选的内容);使正则表达式匹配 800-555-1212以及 (800) 555-1212

 # coding: utf-8

 import re

 n1 = "555-1212"
n2 = "800-555-1212"
n3 = "(800) 555-1212" # 我们要的是整个正则表达式匹配的内容,因此前2个括号括起来
# 的都是不需要的分组,所以用(?: )表示不保存该分组
# '?'表示匹配前面的字符0或1次,'+' 表示匹配前面的字符1或多次
print re.findall(r"(?:\(\d{3}\) )?(?:\d{3}-)+\d{4}", n1)
print re.findall(r"(?:\(\d+\) )?(?:\d+-)+\d+", n2)
print re.findall(r"(?:\(\d+\) )?(?:\d+-)+\d+", n3)

Click