代码一
#! /usr/bin/env python
'''
__Time__ : 2018/8/27 9:30
__Author__ : Damao
__Site__ : Life is short. I use python.
__File__ : test1.py
__Software__ : PyCharm
'''
"""验证输入用户名和QQ号是否有效并给出对应的提示信息"""
import re
def test():
username = input("请输入用户名:")
qq = input("请输入QQ号码:")
match_name = re.match(r'^[0-9a-zA-Z_](8,20)$',username)
if not match_name:
print("输入的用户名格式不正确!")
match_qq = re.match(r'^[1-9]\d[4,10]$',qq)
if not match_qq:
print("输入的QQ号码格式不正确!")
if __name__ == '__main__':
test()
代码二
#! /usr/bin/env python
'''
__Time__ : 2018/8/28 14:02
__Author__ : Damao
'''
import re
def test():
template = re.compile(r'(?<=\D)1[34578]\d{9}(?=\D)')
sentence = '''重要的事情说8130123456789遍,我的手机号是13512346789这个靓号,
不是15600998765,也是110或119,帅帅的手机号才是15600998765。'''
# 查找所有匹配信息并保存到列表中
data_list = re.findall(template,sentence)
print(data_list)
print('--------华丽的分隔线--------')
# 通过迭代器取出匹配对象并获得匹配的内容
for a in template.finditer(sentence):
print(a.group())
# 通过search函数指定搜索位置找出所有匹配
print('--------华丽的分隔线--------')
r = template.search(sentence)
while r:
print(r.group())
r = template.search(sentence,r.end())
if __name__ == '__main__':
test()
代码三
#! /usr/bin/env python
'''
Time = 2018/8/28 14:22
Author = Damao
'''
"""替换字符串中的不良信息"""
import re
def test():
sentence = '你丫是傻叉吗? 我操你大爷的. Fuck you.'
great_sentence = re.sub('[操肏艹草曹]|fuck|shit|傻[比屄逼叉缺吊屌]|煞笔','*',sentence,flags=re.IGNORECASE)
print(great_sentence)
if __name__ == '__main__':
test()
代码四
#! /usr/bin/env python
'''
Time = 2018/8/28 14:35
Author = Damao
'''
"""拆分长字符"""
import re
def test():
poetry = '窗前明月光,疑是地上霜。举头望明月,低头思故乡。'
sentence_list = re.split(r'[,。, .]',poetry)
while '' in sentence_list:
sentence_list.remove('')
print(sentence_list)
if __name__ == '__main__':
test()
网友评论