首页 > 其他 > 详细

【卷一】正则 |> 练习

时间:2016-08-23 22:00:48      阅读:298      评论:0      收藏:0      [点我收藏+]

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

 

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

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

 

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

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

 

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

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

1 # coding: utf-8
2 
3 import re
4 
5 url = "http://www.baidu.com/, http://www.google.cn/, http://www.foothill.edu"
6 
7 # 有多重括号时,如果只需最外面总的分组(一般用括号把我们要的分组给圈起来),那么里面的括号就要加 ?: 表示不保存该分组 
8 print re.findall("(www(?:.+com|.+cn|.+edu))", url)
9 #print re.findall("www.+com|www.+cn|www.+edu", url)

 

【卷一】正则 |> 练习

原文:http://www.cnblogs.com/Ruby517/p/5800907.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!