python自动化之正则
import re
phoneNumRegex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo=phoneNumRegex.search('My number is 415-555-4242.')
print('Phone number found: '+mo.group())
#######利用括号分组##############
phoneNumRegex=re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo=phoneNumRegex.search('My number is 415-555-4242.')
mo.group(1)
mo.group(2)
mo.group(0)
mo.group()
mo.groups()
areaCode,mainNumber=mo.groups()
areaCode
mainNumber
################用管道匹配多个分组#######################
heroRegex=re.compile(r'Batman|Tina Fey')
mo1=heroRegex.search('Batman and Tina Fey')
mo1.group()
mo2=heroRegex.search('Tina Fey and Batman')
mo2.group()
batRegex=re.compile(r'Bat(man|mobile|copter|bat)')
mo=batRegex.search('Batmobile lost a wheel')
mo.group()
mo.group(1)
################用问号实现可选匹配#######################
batRegex=re.compile(r'Bat(wo)?man')
mo1=batRegex.search('The Adventures of Batman')
mo1.group()
mo2=batRegex.search('The Adventures of Batwoman')
mo2.group()
phoneRegex=re.compile(r'(\d\d\d-)?\d\d\d-\d\d\d')
mo1=phoneRegex.search('My number is 415-555-4242')
mo1.group()
mo2=phoneRegex.search('My number is 555-4242')
mo2.group()
################用星号匹配零次或多次###################
batRegex=re.compile(r'Bat(wo)*man')
mo1=batRegex.search('The Adventures of Batman')
mo1.group()
mo2=batRegex.search('The Adventures of Batwoman')
mo2.group()
mo3=batRegex.search('The Adventures of Batwowowoman')
mo3.group()
################用加号匹配一次或多次###################
batRegex=re.compile(r'Bat(wo)+man')
mo1=batRegex.search('The Adventures of Batman')
mo1.group()
mo2=batRegex.search('The Adventures of Batwoman')
mo2.group()
mo3=batRegex.search('The Adventures of Batwowowoman')
mo3.group()
################用花括号匹配特定次数######################
haRegex=re.compile(r'(Ha){3}')
mo1=haRegex.search('HaHaHa')
mo1.group()
mo2=haRegex.search('Ha')
mo2==None
#################贪心和非贪心匹配####################################
##########正则表达式:贪心,在有二义的情况下,尽可能匹配最长的字符串###
greedyHaRegex=re.compile(r'(Ha){3,5}')
mo1=greedyHaRegex.search('HaHaHa')
mo1.group()
nongreedyHaRegex=re.compile(r'(Ha){3,5}?')
mo2=nongreedyHaRegex.search('HaHaHaHa')
mo2.group()
####################findall()方法###################################
###########包含被查找字符串中的所有匹配#############################
phoneNumRegex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
phoneNumRegex.findall('Cell:415-555-9999 Work:215-555-0000')
###########################字符分类#################################
###\d 表示0到9的任何数字
###\D 表示除0到9的数字之外的任何字符
###\w 表示任何字母、数字或下划线字符(可以认为是匹配“单词”字符)
###\W 表示除字母、数字和下划线以外的任何字符
###\s 表示空格、制表符或换行符(可以认为是匹配“单词”字符)
###\S 表示除空格、制表符或换行符以外的任何字符
xmasRegex=re.compile(r'\d+\s\w+')
xmasRegex.findall('12 drummers,11 pipers,10 lords,9 ladies,8 maids,7 swans,6 geese,5 rings,4 birds,3 hens,2 doves,1 partridge')
#######################建立自己的字符分类###########################
vowelRegex=re.compile(r'[aeiouAEIOU]')
vowelRegex.findall('RoboCop eats baby food.BABY FOOD')
########################插入字符和美元字符###########################
#########^表明匹配必须发生在被查找文本开始处#########################
#########$表明该字符串必须匹配该模式结束#############################
###########################通配字符##################################
#########.(句点)字符被称为"通配符"###################################
atRegex=re.compile(r'.at')
atRegex.findall('The cat in the hat sat on the flat mat')
###########################用点-星匹配所有字符######################
######点-星(.*)表示"任意文本"(贪心模式:总是匹配尽可能多的文本)######
####点-星-问号表示“任意文本”(非贪心模式:总是匹配尽可能少的文本)####
nongreedyRegex=re.compile(r'<.*?>')
mo=nongreedyRegex.search('<To serve man> for dinner.>')
mo.group()
greedyRegex=re.compile(r'<.*>')
mo=greedyRegex.search('<To serve man> for dinner.>')
mo.group()
#######################################################################
###############点-星将匹配除换行以外的所有字符#########################
###############通过传入re.DOTALL作为re.compile()的第二个参数###########
###############可以让句点字符匹配所有字符,包括换行字符#################
#######################################################################
##############向re.compile()传入re.IGNORECASE或re.I,不区分大小写#######
#############用sub()方法替换字符串#####################################
namesRegex=re.compile(r'Agent \w+')
namesRegex.sub('CENSORED','Agent Alice gave the secret documents to Agent Bob.')
agentNamesRegex=re.compile(r'Agent (\w)\w*')
agentNamesRegex.sub(r'\1****','Agent Alice told Agent Carol that Agent Eve knew Agent Bob was a double agent.')
####################管理复杂的正则表达式###############################
phoneRegex=re.compile(r'''(
(\d{3}|\(\d{3}\))? #area code
(\s|-|\.)? #separator
\d{3} #first 3 digits
(\s|-|\.) #separator
\d{4} #last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? #extension
)''',re.VERBOSE)
#########################################################################
######################pyperclip模块复制和粘贴字符串######################
import pyperclip,re
####Create phone regex
phoneRegex=re.compile(r'''(
(\d{3}|\(\d{3}\))? #area code
(\s|-|\.)? #separator
\d{3} #first 3 digits
(\s|-|\.) #separator
\d{4} #last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? #extension
)''',re.VERBOSE)
####Create email regex
emailRegex=re.compile(r'''(
[a-zA-Z0-9._%+-]+ #username
@ #@ symbol
[s-zA-Z0-9.-]+ #domain name
(\.[a-zA-Z]{2,4}) #dot-something
)''',re.VERBOSE)
####Find matches in clipboard text.
text=str(pyperclip.paste())
matches=[]
for groups in phoneRegex.findall(text):
phoneNum = '-'.join([groups[1],groups[3],groups[5]])
if groups[8] !='':
phoneNum += ' x'+groups[8]
matches.append(phoneNum)
####Copy results to the clipboard.
if len(matches)>0:
pyperclip.copy('\n'.join(matches))
print('Copied to clipboard:')
print('\n'.join(matches))
else:
print('No phone numbers or email addresses found.')
###示例1:
##############################强口令检测###################
###################长度不少于8个字符#######################
###################同时包含大写和小写字符##################
###################至少有一位数字##########################
import re
def checkLength(pwd):
IfOrNot=len(pwd)
if IfOrNot>=8:
return True
else:
return False
def checkUpperLetter(pwd):
UpperLetter=re.compile(r'[A-Z]+')
mo=UpperLetter.search(pwd)
if mo:
return True
else:
return False
def checkLowerLetter(pwd):
LowerLetter=re.compile(r'[a-z]+')
mo=LowerLetter.search(pwd)
if mo:
return True
else:
return False
def checkNumLetter(pwd):
LowerLetter=re.compile(r'[0-9]+')
mo=LowerLetter.search(pwd)
if mo:
return True
else:
return False
def checkPassword(pwd):
return (checkLength(pwd) and checkUpperLetter(pwd) and checkLowerLetter(pwd) and checkNumLetter(pwd))
###示例2:
####正则表达式,匹配每3位就有一个逗号的数字?必须匹配以下数字:
.'42'
.'1,234'
.'6,368,745'
####但不会匹配:
.'12,34,567'
.'1234'
numRegex=re.compile(r'^\d{1,3}(,\d{3})*$')
mo=numRegex.search('12,304,567')
mo.group()
python自动化之正则的更多相关文章
- python自动化运维之路~DAY5
python自动化运维之路~DAY5 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.模块的分类 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数 ...
- python浅谈正则的常用方法
python浅谈正则的常用方法覆盖范围70%以上 上一次很多朋友写文字屏蔽说到要用正则表达,其实不是我不想用(我正则用得不是很多,看过我之前爬虫的都知道,我直接用BeautifulSoup的网页标签去 ...
- python匹配ip正则
python匹配ip正则 #!/usr/bin/env python # -*- coding:utf-8 -*- import re ip_str = "asdad1.1.1.1sdfwe ...
- flow.ci + Github + Slack 一步步搭建 Python 自动化持续集成
理想的程序员必须懒惰,永远追随自动化法则.Automating shapes smarter future. 在一个 Python 项目的开发过程中可能会做的事情:编译.手动或自动化测试.部署环境配置 ...
- Selenium2+python自动化23-富文本(自动发帖)
前言 富文本编辑框是做web自动化最常见的场景,有很多小伙伴遇到了不知道无从下手,本篇以博客园的编辑器为例,解决如何定位富文本,输入文本内容 一.加载配置 1.打开博客园写随笔,首先需要登录,这里为了 ...
- Selenium2+python自动化24-js处理富文本(带iframe)
前言 上一篇Selenium2+python自动化23-富文本(自动发帖)解决了富文本上iframe问题,其实没什么特别之处,主要是iframe的切换,本篇讲解通过js的方法处理富文本上iframe的 ...
- Selenium2+python自动化7-xpath定位
前言 在上一篇简单的介绍了用工具查看目标元素的xpath地址,工具查看比较死板,不够灵活,有时候直接复制粘贴会定位不到.这个时候就需要自己手动的去写xpath了,这一篇详细讲解xpath的一些语法. ...
- Selenium2+python自动化13-Alert
不是所有的弹出框都叫alert,在使用alert方法前,先要识别出它到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决.alert\confirm\prompt弹出框 ...
- 【python自动化第十一篇】
[python自动化第十一篇:] 课程简介 gevent协程 select/poll/epoll/异步IO/事件驱动 RabbitMQ队列 上节课回顾 进程: 进程的诞生时为了处理多任务,资源的隔离, ...
随机推荐
- jmeter no-gui模式动态传递场景参数
jmeter进行性能压测时,有时候需要在linux上no-gui模式下运行,为了在no-gui模式下更方便的设置脚本的运行的场景, 将脚本的线程数,运行时间设置为动态参数,可以在脚本运行时动态设置“线 ...
- Zabbix实战-简易教程--订阅类
一.需求提出 最近数据中心有一个新的需求,有一批后台任务需要在每天固定时间点运行(凌晨8:00),现在希望能够把这个任务执行的结果定时上报给他. 说明:执行的任务为一个sql查询,查询出来的是每个任务 ...
- 图片缩放插件GestureImageView——Android 常用插件推荐(一)
Android 开发过程中,交互效果是一个非常繁琐的过程,甚至比Web开发过程中JS特效更加复杂.通过多年的发展,常用的交互方式已经发展相当成熟,而且有很多非常好的插件.为了避免重复造轮子,一些常用的 ...
- Jmeter关联处理
采桑子·重阳 人生易老天难老, 岁岁重阳. 今又重阳, 战地黄花分外香. 一年一度秋风劲, 不似春光. 胜似春光, 廖廓江天万里霜. 当请求之间有依赖关系,比如一个请求的入参是另一个请求返回的数据,这 ...
- 服务发现与消费 --> Spring Cloud Eureka
在上两篇文章中,我们已经搭建起微服务架构中的核心组件 服务注册 中心(包括单节点模式和高可用模式).同时, 还对上一章中实现的Spring Boot入门程序 做了改造. 通过简单的配置,使该程序注册到 ...
- eclipse创建spring boot项目加载不到application.properties配置文件
在配置文件application.properties中修改了端口号,但重启服务后发现端口号并没有跟着改变,发现是项目启动时没有加载application.properties文件导致 解决:项目-& ...
- LeetCode-63.不同路径Ⅱ
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网 ...
- 03_set slice的时间复杂度
set slice O(n+k) 使用切片赋值来解释set slice的时间复杂度 (1) 对li[0:3]赋值首先会删除1,2,3,空出来的位置被后面的元素依次向前移动填充,由del slice 得 ...
- zip命令详解
基础命令学习目录首页 好文链接:https://www.cnblogs.com/yinzhengjie/p/6247833.html 原文链接:https://www.cnblogs.com/ferr ...
- Java-URLEncoder.encode 什么时候才是必须的
当你希望把一段 URL 当成另一个 URL 的参数时,比如:当用户点击交易的按钮时你发现未登录就跳转到 login 页面同时带上一个参数记录在登录之前用户是希望访问的那个交易页面,这样在登录完成之后再 ...