python之正则表达式

#正则表达式是记录文本规则的代码
#是一个特殊的字符序列
#普通字符和元字符组成的。其实就是对元字符的学习

import re
#python中特殊的库,包含正则表达式,元字符串等
reg_string = "hello6232pyeh@qwhg.@!hellohh"
reg = "hello"
result = re.findall(reg,reg_string)
print(result)


'''
元字符
常用的元字符
.匹配换行符以外的任意字符
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
'''
reg_string = "hello62_王 32 pyeh@qwhg.@!hellohh"

reg = "."
s = re.findall(reg,reg_string)
print(s)

reg = "\d"
s = re.findall(reg,reg_string)
print(s)

reg = "\w"
s = re.findall(reg,reg_string)
print(s)

reg = "\s"
s = re.findall(reg,reg_string)
print(s)

reg = "\b"#?????????????????????????
s = re.findall(reg,reg_string)
print(s)

reg = "^hello"
s = re.findall(reg,reg_string)
print(s)

reg = "hh$"
s = re.findall(reg,reg_string)
print(s)

#反义代码
'''
.匹配换行符以外的任意字符
\W 匹配任意不是字母或数字或下划线或汉字
\S 匹配任意不是空白符的字符
\D 匹配非数字
\B 匹配不是单词开始或结束的位置
[^a] 匹配除了a以外的任意字符
'''

reg = "\W"
s = re.findall(reg,reg_string)
print(s)

#限定符
'''
* 重复零次或多次
+ 重复1次或多次
? 重复零次或一次
{n} 重复n次
{n,} 重复n次到更多次数
{n,m} 重复n到m次

'''

reg_string = "hello6232pyeh@qwhg.@!hellohh"
reg = "\d{4}"#理解为找到长度为4的数字存于列表的一个元素中
result = re.findall(reg,reg_string)
print(result)

reg = "[0-9a-z]{4}"#匹配范围及长度
result = re.findall(reg,reg_string)
print(result)

#练习
ip = "this is ip:192.168.1.123 :172.138.2.15"
reg = "\d{3}.\d+.\d+.\d+"
result = re.findall(reg,ip)
print(result)

#search
ip = "this is ip:192.168.1.123 :172.138.2.15"
reg = "(\d{1,3}.){3}\d{1,3}"#改规则下使用findall,只找到重复为1次的
result = re.search(reg,ip)#返回一个数组类型(list)
print(result[0])

'''
search 和 findall
search只匹配第一个
findall匹配所有
'''

#组匹配
s1="this is my phone :13888888888 and this is my postcode :012345"
reg1 ="this is my phone :(\d{11}) and this is my postcode :(\d{6})"
result = re.search(reg1 ,s1)
print(result)
result = re.search(reg1 ,s1).group(0)
print(result)
result = re.search(reg1 ,s1).group(1)
print(result)
result = re.search(reg1 ,s1).group(2)
print(result)

reg_string = "HellosdawedHello"
reg = "Hello"
result = re.match(reg,reg_string).group()
#match只匹配开头,开头不是Hello则返回错误
print(result)

reg_string = "hellosdawedHello"
reg = "Hello"
result = re.match(reg,reg_string,re.I)
#match只匹配开头,re.I:忽略大小写
print(result[0])

result = re.match(reg,reg_string,re.I).group()
#match只匹配开头,re.I:忽略大小写
print(result)


#贪婪与非贪婪
'''
贪婪:尽可能多的匹配
非贪婪:尽可能少的匹配
非贪婪符号:? 用在* + ?后边,要求正则表达式匹配尽可能少
* 重复零次或多次
+ 重复1次或多次
? 重复零次或一次
*? 重复零次
+? 重复1?? 重复零次
'''
#贪婪
reg_string = "pythonnnnsdsapythonsdsa1112"
reg = "python+"
result = re.findall(reg,reg_string)
print(result)

#非贪婪
reg = "python+?"
result = re.findall(reg,reg_string)
print(result)

reg = "python*?"
result = re.findall(reg,reg_string)
print(result)

reg = "python??"
result = re.findall(reg,reg_string)
print(result)


#手机号码验证
'''
移动:139 138 137 151
联通:130 131
电信:189 180
'''
def checkCellphone(cellphone):
    regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$"
    result = re.findall(regex,cellphone)
    if result:
        print("success")
        return True
    else:
        print("fail")
        return False
cellphone = "13113114251"
print(checkCellphone(cellphone))







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值