python教科书正则表达式答案_Python课程第⼗⼀章正则表达
式习题详解
Python课程第⼗⼀章正则表达式习题详解
1、匹配⼀⾏⽂字中的所有开头的字母内容
>>> import re
>>> re.match(r"\D+?","Abc")
>>> re.match(r"\D+?","Abc").group()
'A'
>>> re.match(r"\D+","A12345")
>>> re.match(r"\D+","A12345").goup()
Traceback (most recent call last):
File "", line 1, in
AttributeError: '_sre.SRE_Match' object has no attribute 'goup'
>>> re.match(r"\D+","A12345").group()
《java完全自学手册》'A'
2、匹配⼀⾏⽂字中的所有开头的数字内容
>>> re.match(r"\d+?","1234567")
>>> re.match(r"\d+?","1234567").group()
'1'
>>> re.match(r"\d+","1234567").group()
'1234567'
>>> re.match(r"\d+","1ascdfgh1234").group()
'1'
>>>
3、匹配⼀⾏⽂字中的所有开头的数字内容或数字内容
4、 只匹配包含字母和数字的⾏
>>> re.search(r'\w+[a-zA-Z0-9]',"acvbghj12345*").group()
'acvbghj12345'
>>> re.search(r'\w[a-zA-Z0-9]+',"acvbghj12345*").group()
'acvbghj12345'
5、写⼀个正则表达式,使其能同时识别下⾯所有的字符串:'bat',
'bit', 'but', 'hat', 'hit', 'hut‘
>>> re.match(r"\b[bh][aiu]t","bat bit but hat hit hut")
>>> re.match(r"\b[bh][aiu]t","bat bit but hat hit hut").group()
'bat'
>>> re.findall(r"\b[bh][aiu]t","bat bit but hat hit hut")
['bat', 'bit', 'but', 'hat', 'hit', 'hut']
>>>
6、匹配所有合法的python标识符
>>> pattern='[a-zA-Z_][\w_]+'
7、提取每⾏中完整的年⽉⽇和时间字段
>>> test_datetime = u'他的⽣⽇是2016-12-12 14:34,是个可爱的⼩宝贝.⼆宝的⽣⽇16-12-21 11:34,好可爱的.'
>>> pattern=repile(r'\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}\:\d{1,2}')
>>> search=pattern.search(test_datetime)
>>> search=pattern.search(test_datetime)
>>> up()
2016-12-12 14:34python教程电子版书籍
>>> match=pattern.match(test_datetime)
>>> up()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'NoneType' object has no attribute 'group'
>>> findall=pattern.findall(test_datetime)
>>> print findall
[u'2016-12-12 14:34', u'2016-12-21 11:34']
>>> for datetime in findall:
... print datetime
...
2016-12-12 14:34
2016-12-21 11:34
>>>>>> a="123yuye2345"
>>> pattern=repile(r'[0-9].*')
php和servletTraceback (most recent call last):
File "", line 1, in
NameError: name 're' is not defined
>>> import re
>>> a="123yuye2345"
>>> pattern=repile(r'[0-9].*')
>>> result=pattern.findall(a)
>>> print result
['123yuye2345']
>>> result=pattern.search(a)
>>> print result
>>> up()
123yuye2345
>>> result=pattern.match(a)
>>> print result
>>> up()
123yuye2345
>>>
8、将每⾏中的电⼦邮件地址替换为你⾃⼰的电⼦邮件地址
9、匹配\home关键字:
进阶练习
1、使⽤正则提取出字符串中的单词
>>> a="qwer qwer sdf cvvb"
>>> pattern='[a-zA-Z]+'
>>> re.findall(pattern,a)
['qwer', 'qwer', 'sdf', 'cvvb']
2、使⽤正则表达式匹配合法的邮件地址:
>>> add = 'www.hp.ibm.edu'
>>> pattern=repile(r'((w{3}\.)(\w+\.)+(com|edu|cn|net))')
>>> result=pattern.findall(add)
>>> print result
[('www.hp.ibm.edu', 'www.', 'ibm.', 'edu')]
>>> re.findall(r'((w{3}\.)(\w+\.)+(com|edu|cn|net))',add)创建网站首页教案
[('www.hp.ibm.edu', 'www.', 'ibm.', 'edu')]
3、国际域名格式如下:
域名由各国⽂字的特定字符集、英⽂字母、数字及“-”(即连字符或减号)任意组合⽽成, 但开头及结尾均不能含有“-”,“-”不能连续出现。域名中字母不分⼤⼩写。域名最长可达60个字节(包括后缀、、.org等)。
4、提取字符串中合法的超链接地址
⽐如:s = ''
要求,给出的正则表达式能兼顾所有链接地址。
>>> s=''
>>> matchResult=re.search(r'(.*)',s)
>>> up()
'
xf8'
>>> up(1)
'\xb9\xe2\xc8\xd9\xd6\xae\xc2\xb7\xb9\xd9\xcd\xf8' >>> matchResult=re.search(r'(.*)',s).group(1)
>>> print group(1)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'group' is not defined
>>> print matchResult
光荣之路官⽹
>>> up()
Traceback (most recent call last):
File "", line 1, injs获取对象属性值的方法
AttributeError: 'str' object has no attribute 'group'
>>> s=''
>>> matchResult=re.search(r'(.*)',s)
>>> up()
>>>
5、统计⽂件中单词个数
6、写⼀个函数,其中⽤正则验证密码的强度
#coding=utf-8
import re
def checklen(pwd):
return len(pwd)>=8
def checkContainUpper(pwd):
pattern=repile('[A-Z]+')
match=pattern.findall(pwd)
def checkContainUpper(pwd): pattern=repile('[A-Z]+')
match=pattern.findall(pwd)
if match:
return True
else:
return False
def checkContainNum(pwd):
pattern=repile('[0-9]+')
match=pattern.findall(pwd)
if match:
return True
else:
return False
def checkContainLower(pwd): pattern=repile('[a-z]+')
match=pattern.findall(pwd)
if match:
return True
else:
return False
def checkSymbol(pwd):
pattern=repile('[^a-z0-9A-Z]+') match=pattern.findall(pwd)
if match:
return True
else:
return False
def checkPassword(pwd):
lenOK=checklen(pwd)
upperOK=checkContainUpper(pwd) lowerOK=checkContainLower(pwd) numOK=checkContainNum(pwd) symbolOK=checkSymbol(pwd)
>回车键的通配符是什么
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论