1. 程式人生 > >python-正則表達式練習題一

python-正則表達式練習題一

標識 sin woe mat python2 完整 地址 開頭 自己

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‘, ‘y‘, ‘a‘, ‘b‘, ‘b‘, ‘o‘, ‘w‘, ‘i‘, ‘a‘, ‘w‘, ‘i‘, ‘a‘, ‘w‘, ‘y‘]

2、匹配一行文字中的所有開頭的數字內容

import re

s="i love you not because 12sd 34er 56df e4 54434"

content=re.findall(r"\b\d",s)

print content

c:\Python27\Scripts>python task_test.py

[‘1‘, ‘3‘, ‘5‘, ‘5‘]

3、匹配一行文字中的所有開頭的數字內容或數字內容

>>> print re.match(r"\w+","123sdf").group()

123sdf

4、 只匹配包含字母和數字的行

#coding=utf-8

import re

s="i love you not because\n12sd 34er 56\ndf e4 54434"

content=re.findall(r"\w+",s,re.M)

print content

c:\Python27\Scripts>python task_test.py

[‘i‘, ‘love‘, ‘you‘, ‘not‘, ‘because‘, ‘12sd‘, ‘34er‘, ‘56‘, ‘df‘, ‘e4‘, ‘54434‘]

5、寫一個正則表達式,使其能同時識別下面所有的字符串:‘bat‘, ‘bit‘, ‘but‘, ‘hat‘, ‘hit‘, ‘hut‘

import re

s="‘bat‘, ‘bit‘, ‘but‘, ‘hat‘, ‘hit‘, ‘hut"

content=re.findall(r"..t",s)

print content

c:\Python27\Scripts>python task_test.py

[‘bat‘, ‘bit‘, ‘but‘, ‘hat‘, ‘hit‘, ‘hut‘]

6、匹配所有合法的python標識符

#coding=utf-8

import re

s="awoeur awier !@# @#4_-asdf3$^&()+?><dfg$\n$"

content=re.findall(r".*",s,re.DOTALL)

print s

print content

c:\Python27\Scripts>python task_test.py

awoeur awier !@# @#4_-asdf3$^&()+?><dfg$

$

[‘awoeur awier !@# @#4_-asdf3$^&()+?><dfg$\n$‘, ‘‘]

7、提取每行中完整的年月日和時間字段

#coding=utf-8

import re

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}",s,re.M)

print s

print content

c:\Python27\Scripts>python task_test.py

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、將每行中的電子郵件地址替換為你自己的電子郵件地址

#coding=utf-8

import re

s="""[email protected], [email protected], [email protected]

[email protected], [email protected]

[email protected]"""

content=re.sub(r"\w+@\w+.com","[email protected]",s)

print s

print "_______________________________________"

print content

c:\Python27\Scripts>python task_test.py

[email protected], [email protected], [email protected]

[email protected], [email protected]

[email protected]

_______________________________________

[email protected], [email protected], [email protected]

[email protected], [email protected]

[email protected]

9、匹配\home關鍵字:

>>> re.findall(r"\\home","skjdfoijower \home \homewer")

[‘\\home‘, ‘\\home‘]

1、使用正則提取出字符串中的單詞

#coding=utf-8

import re

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",s)

print content

c:\Python27\Scripts>python task_test.py

[‘i‘, ‘love‘, ‘you‘, ‘not‘, ‘because‘, ‘of‘, ‘who‘, ‘you‘, ‘are‘, ‘but‘, ‘because‘, ‘of‘, ‘who‘, ‘i‘, ‘am‘, ‘when‘, ‘i‘, ‘am‘, ‘with‘, ‘you‘]

2、使用正則表達式匹配合法的郵件地址:

import re

s="""[email protected], [email protected] [email protected] [email protected] [email protected] saldkfj.com [email protected]"""

content=re.findall(r"\w+@\w+.com",s)

print content

c:\Python27\Scripts>python task_test.py

[‘[email protected]‘, ‘[email protected]‘, ‘[email protected]‘, ‘[email protected]‘]

python-正則表達式練習題一