python正则表达式试题_正则表达式练习题2
1、匹配一行文字中的所有開頭的字母內容
#coding=utf-8
import re
s="i love you not because of who you are,but because of who i am when i am with you"
content=re.findall(r"\b\w",s)
print content
c:\Python27\Scripts>python task_test.py
[‘i‘,‘l‘,‘y‘,‘n‘,‘b‘,‘o‘,‘w‘,‘a‘,‘i‘,‘y‘]
2、匹配一行文字中的所有開頭的數字內容
s="i love you not because 12sd 34er 56df e4 54434"
content=re.findall(r"\b\d",sans-serif; background-color: #faf7ef;">c:\Python27\Scripts>python task_test.py
[‘1‘,‘3‘,‘5‘,‘5‘]
3、匹配一行文字中的所有開頭的數字內容或數字內容
>>> print re.match(r"\w+","123sdf").group()
123sdf
4、 只匹配包含字母和數字的行
s="i love you not because\n12sd 34er 56\ndf e4 54434"
content=re.findall(r"\w+",s,re.M)
5、寫一個正則表達式,使其能同時識別下面所有的字符串:‘bat‘,‘bit‘,‘but‘,‘hat‘,‘hit‘,‘hut‘
s="‘bat‘,‘hut"
content=re.findall(r"..t",sans-serif; background-color: #faf7ef;">[‘bat‘,‘hut‘]
6、匹配所有合法的python標識符
s="awoeur awier [email?protected]# @#4_-asdf3$^&()+?>
content=re.findall(r".*",re.DOTALL)
print s
awoeur awier [email?protected]# @#4_-asdf3$^&()+?>
$
[‘awoeur awier [email?protected]# @#4_-asdf3$^&()+?>
7、提取每行中完整的年月日和時間字段
s="""se234 1987-02-09 07:30:00
1987-02-10 07:25:00"""
content=re.findall(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}",sans-serif; background-color: #faf7ef;">se234 1987-02-09 07:30:00
1987-02-10 07:25:00
[‘1987-02-09 07:30:00‘,‘1987-02-10 07:25:00‘]
8、將每行中的電子郵件地址替換為你自己的電子郵件地址
content=re.sub(r"\[email?protected]\w+.com","[email?protected]",sans-serif; background-color: #faf7ef;">print "_______________________________________"
_______________________________________
9、匹配\home關鍵字:
>>> re.findall(r"\\home","skjdfoijower \home?? \homewer")
[‘\\home‘,‘\\home‘]
1、使用正則提取出字符串中的單詞
s="""i love you not because of who 234 you are,234 but 3234ser because of who i am when i am with you"""
content=re.findall(r"\b[a-zA-Z]+\b",‘of‘,‘who‘,‘are‘,‘am‘,‘when‘,‘with‘,‘you‘]
2、使用正則表達式匹配合法的郵件地址:
總結
以上是生活随笔為你收集整理的python正则表达式试题_正则表达式练习题2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何下载Chrome谷歌浏览器历史版本
- 下一篇: python每隔半个小时执行一次_一篇文