python 正则表达式练习

python 正则表达式练习

匹配单个字符

在这里插入图片描述

匹配多个字符

在这里插入图片描述

匹配开头和结尾

在这里插入图片描述

匹配分组

在这里插入图片描述

import re

def test(law,text):

	data=re.match(law,text)
	if data:
		print("发现匹配:")
		print(data.group())
	else:
		print("%s没有发现匹配在%s"%(law,text))
# match 只匹配一个字符串开始的位置
help(re.match)
test(".","M")
test("t.o","too")
test("t.o","two")
# 如果hello的首字符小写,那么正则表达式需要小写的h
test("h","hello Python")
# 如果hello的首字符大写,那么正则表达式需要大写的H
test("H","Hello Python")
# 大小写h都可以的情况
test("[hH]","hello Python")
test("[hH]","Hello Python")
# 匹配0到9第一种写法
test("[0123456789]Hello Python","7Hello Python")
# 匹配0到9第二种写法
test("[0-9]Hello Python","1Hello Python")
test("[0-9][0-9]Hello Python","10Hello Python")
# 下面这个正则不能够匹配到数字4
test("[0-3 5-9]Hello Python","4Hello Python")
# 能够匹配到空格
test("[0-3 5-9]Hello Python"," Hello Python")

test("嫦娥1号","嫦娥1号发射成功")
test("嫦娥\\d号","嫦娥1号发射成功")
test("嫦娥\\d号","嫦娥2号发射成功")
test("嫦娥\\d号","嫦娥3号发射成功")

test("[A-Z][a-z]*","M")
test("[A-Z][a-z]*","Mnnnnnnnn")
test("[A-Z][a-z]*","M1111nnnn")

#需求:匹配出,变量名是否有效
names=["name1", "_name", "2_name", "__name__"]
for x in names:
	test("[a-zA-Z_]+[\\w]",x)

#需求:匹配出,0到99之间的数字
test("[1-9]?[0-9]","7")
test("[1-9]?[0-9]","79")

#需求:匹配出,8到20位的密码,可以是大小写英文字母、数字、下划线
test("[a-zA-Z0-9_]{6}","12a3g45678")
test("[a-zA-Z0-9_]{8,20}","1ad12f23s34455ff661ad12f23s34455ff66")


#匹配出163的邮箱地址,且@符号之前有4到20位,例如hello@163.com
emails=["f661ad1@163.com","24899988389489@qq.com","f661@163.com","f661ad1fefddwe12345555@126.com"]
for x in emails:
	test("[a-zA-Z0-9_]{4,20}[@][163]{3}[.com]{4}",x)
for x in emails:
	test("\\w{4,20}@163.com",x)



email_list = ["xiaoWang@163.com", "xiaoWang@163.comheihei", ".com.xiaowang@qq.com"]
#非163结尾的会出现错误
for x in email_list:
	test("\\w{4,20}@163.com",x)
#修正后
for x in email_list:
	test("\\w{4,20}@163.com$",x)


#需求:匹配出0-100之间的数字
test("[1-9]?\\d","8")
#不正确
test("[1-9]?\\d","08")
test("[1-9]?\\d","100")
#修正
test("[1-9]?\\d$|100","08")
test("[1-9]?\\d$|100","100")

#需求:匹配出163、126、qq邮箱
email_list = ["xiaohong@163.com","xiaoWang@126.com", "xiaoWang@163.comheihei", ".com.xiaowang@qq.com"]
for x in email_list:
	test("\\w{4,20}@(163|126|qq).com$",x)

#不是以4、7结尾的手机号码(11位)
phone=["cc18217389576","18217389677","18217389684","111111118738921680","18289680177","10086"]
for x in phone:
	test("\\d{10}[47]",x)


#需求:匹配出<html>hh</html>
test("<[a-zA-Z]*>\\w*</[a-zA-Z]*>","<html>hh</html>")
# test("<[a-zA-Z]*>\\w*</[a-zA-Z]*>","")
#遇到非正常HTML时出错
test("<[a-zA-Z]*>\\w*</[a-zA-Z]*>","<htmlssssdf>hh</htmlssssdf>")
# 正确的理解思路:如果在第一对<>中是什么,按理说在后面的那对<>中就应该是什么
# 通过引用分组中匹配到的数据即可
#\1是引用()分组的数据 第几组数字就是几
test("<([a-zA-Z]*)>\\w*</\\1><([a-zA-Z]*)>\\w*</\\2>","<html>hh</html><htmlssssdf>hh</htmlssssdf>")
test("<(\\w*)><(\\w*)></\\2></\\1>","<a><i></i></a>")
#分组重命名 注意:(?P<name>)和(?P=name)中的字母p大写
test("<(?P<name>\\w*)><(?P<na>\\w*)></(?P=na)></(?P=name)>","<a><i></i></a>")

测试文件123.txt

itcast hello python
itcast c++
itheima ios
itheima php

hello itcast python
www.itcast.cn c++
itheima ios
itheima php

要求在一个文件中,查找出itcast开头的语句,和含有itcast的语句

import re

def read(file_name):
	try:
		f=open(file_name)
	except Exception as e:
		return ""
	else:
		f_t=f.read()
		print("读取到:\n%s"%f_t)
		return f_t
	

def flie_match(law,file_txt):
	ret=re.match(law,file_txt)
	# help(ret)
	if ret:
		print("发现匹配:%s"%file_txt)
		# for x in ret.groups():
		# 	
		
	else:
		print("%s没有发现匹配:%s"%(law,file_txt))

#查找出itcast开头的语句,和含有itcast的语句
file_txt=read("123.txt").splitlines()
print("返回:%s\n"%file_txt)
#itcast开头的语句
for x in file_txt:
	
	flie_match("^itcast",x)
print("*"*50)
#含有itcast的语句
for x in file_txt:
	#itcast开头的语句
	flie_match(".*itcast.*",x)	
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值