python---正则表达式

正则表达式

import re

reg_string = "hello9527python@wangcai.@!:xiaoqiang"
reg = "hello"

result = re.findall(reg, reg_string)
print(result)

在这里插入图片描述

元字符

import re

'''
. 匹配换行符之外的任意字符
\w 匹配字母或数组或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
'''

reg_string = "hello9527python@小强hello"

result = re.findall('\d', reg_string)
print(result)

result = re.findall('^hello', reg_string)
print(result)

result = re.findall('\w', reg_string)
print(result)

在这里插入图片描述

反义代码

import re

'''
. 匹配换行符之外的任意字符
\W 匹配任意不是字母、数组、汉字的字符
\S 匹配任意不是空白的字符
\D 匹配非数字
\B 匹配不是单词的开始或结束的位置
[^a] 匹配除了a以外的任意字符
'''

reg_string = "hello9527python@小强hello"

result = re.findall('\D', reg_string)
print(result)

result = re.findall('[^h]', reg_string)
print(result)

result = re.findall('\W', reg_string)
print(result)

在这里插入图片描述

限定符

import re

'''
* 重复零次或多次
+ 重复一次或多次
? 重复零次或一次
{n} 重复 n 次
{n, } 重复 n 次或更多次
{n, m} 重复 n 到 m 次
'''

reg_string = "hello9527python@小强hello"

result = re.findall('\d{4}', reg_string)
print(result)

result = re.findall('[0-9a-z]{4}', reg_string)
print(result)

在这里插入图片描述

练习

import re

ip = "this is ip:192.168.2.123 : 172.138.2.15"
result = re.findall("\d{3}.\d+.\d+.\d+", ip)
print(result)

'''
search 只匹配第一个
findall 匹配所有
'''
result = re.search("(\d{1,3}.){3}\d{1,3}", ip)[0]
print(result)

在这里插入图片描述

组匹配

import re

s = "this is phone:13888888888 and this is my postcode:012345"
reg = "this is phone:(\d{11}) and this is my postcode:(\d{6})"
result = re.search(reg, s).group(0)
print(result)

result = re.search(reg, s).group(1)
print(result)

result = re.search(reg, s).group(2)
print(result)

# match 只匹配开头
reg_string = "HelloPythonString"
result = re.match("Hello", reg_string).group()
print(result)

# re.I 忽略大小写
reg_string = "helloPythonString"
result = re.match("Hello", reg_string, re.I).group()
print(result)

在这里插入图片描述

贪婪与懒惰

import re

'''
贪婪:尽可能多的匹配
懒惰:尽可能少的匹配
懒惰操作符:?,位于 * + 后面,要求正则匹配越少越好
* 重复零次或多次
+ 重复一次或多次
? 重复零次或一次
'''

reg_string = "pythonnnnnnnnnpythonHelloPython"

# 贪婪
result = re.findall("python*", reg_string)
print(result)

# 懒惰
result = re.findall("python*?", reg_string)
print(result)

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

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

在这里插入图片描述

练习题

import re

'''
移动:139,138,137,136,135,134
150,151,152,157,158,159
182,183,187,188,147
联通:130,131,132,185,186,145
电信:133,153,180,189
'''

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("匹配成功")
        return True
    else:
        print("匹配失败")
        return False
    
cellphone = "13987656789"
print(checkCellphone(cellphone))
    
cellphone = "139876567891"
print(checkCellphone(cellphone))

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值