python-正则表达式练习题一

Posted 夏晓旭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-正则表达式练习题一相关的知识,希望对你有一定的参考价值。

 

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 [email protected]# @#4_-asdf3$^&()+?><dfg$\n$"

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

print s

print content

c:\Python27\Scripts>python task_test.py

awoeur awier [email protected]# @#4_-asdf3$^&()+?><dfg$

$

[‘awoeur awier [email protected]# @#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"\[email protected]\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"\[email protected]\w+.com",s)

print content

c:\Python27\Scripts>python task_test.py

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

以上是关于python-正则表达式练习题一的主要内容,如果未能解决你的问题,请参考以下文章

python正则表达式练习篇

第一章:正则表达式

Python正则表达式练习

Python实战之正则表达式RE/re学习笔记及简单练习

Python正则表达式练习

python-正则表达式练习