python正则表达式re.match()匹配多个字符方法的实现


Posted in Python onJanuary 27, 2021

1.  *表示匹配任意多个字符   \d*表示匹配任意多个数字字符

import re
 
text = "123h1ello world"
text1 = "123Hello world456"
text2 = "hello world"
 
res = re.match("\d*", text)  
res1 = re.match("\d*", text1)
res2 = re.match("\d*", text2)
 
print(res.group())
print(res1.group())
print(res2.group())

输出结果为

123
123

Process finished with exit code 0

示例2:*

需求:匹配出,一个字符串第一个字母为大小字符,后面都是小写字母并且这些小写字母可有可无

import re
#注意是前一个字符
ret = re.match("[A-Z][a-z]*","M")
print(ret.group())

ret = re.match("[A-Z][a-z]*","AaBcDE")
print(ret.group())

ret = re.match("[A-Z][a-z]*","Aabcdef")
print(ret.group())

#运行结果
M
Aa
Aabcdef

2.  +表示匹配1个或者多个任意字符   \w+表示匹配一个或多个字母,数字或下划线

import re
 
text = "he+llo world"
text1 = "Hello world456"
text2 = "+hello world"
 
res = re.match("\w+", text)
res1 = re.match("\w+", text1)
res2 = re.match("\w+", text2)
 
print(res.group())
print(res1.group())
print(res2)

输出结果为

he
Hello
None

Process finished with exit code 0

示例2:+

需求:匹配出,变量名是否有效

import re
names = ["name1","_name","2_name","__name__"]
for i in names:
  ret = re.match("[a-zA-Z_]+[\w]*",i)
  if ret:
    print("变量名 %s 符合要求" % ret.group())
  else:
    print("变量名 %s 非法" % i)

#运行结果
变量名 name1 符合要求
变量名 _name 符合要求
变量名 2_name 非法
变量名 __name__ 符合要求

3.  ?表示匹配0个或一个字符     \w?表示匹配0或1个字母,数字或下划线

import re
 
text = "he+llo world"
text1 = "Hello world456"
text2 = "+hello world"
 
res = re.match("\w?", text)
res1 = re.match("\w?", text1)
res2 = re.match("\w?", text2)
 
print(res.group())
print(res1.group())
print(res2.group())

输出结果为

h
H

Process finished with exit code 0

示例2:?

需求:匹配出0到99之间的数字

import re

ret = re.match("[1-9]?[0-9]","7")
print(ret.group())

ret = re.match("[1-9]?\d","33")
print(ret.group())

ret = re.match("[1-9]?\d","09")
print(ret.group())

#运行结果
7
33
0 # 这个结果并不是想要的,利用$才能解决

4.  {m}表示匹配m个字符    \d{11}表示匹配11个数字字符

import re
 
text = "he+llo world"
text1 = "Hello world456"
text2 = "hello world"
 
res = re.match("\w{2}", text)
res1 = re.match("\w{3}", text1)
res2 = re.match("\w{4}", text2)
 
print(res.group())
print(res1.group())
print(res2.group())

输出结果为

he
Hel
hell

Process finished with exit code 0

示例2:{m}
需求:匹配出,8到20位的密码,可以是大小写英文字母、数字、下划线

import re
ret = re.match("[a-zA-Z0-9_]{6}","123a3g45678")
print(ret.group())

ret = re.match("[[a-zA-Z0-9_]{8,20}","1ad3123456addfcasdef")
print(ret.group())

#运行结果
123a3g
1ad3123456addfcasdef

5.   {m,n}表示匹配m-n个字符   \w{2,4}表示匹配2-4个字符

import re
 
text = "hello world"
text1 = "Helloworld456"
text2 = "hello world"
 
res = re.match("\w{2,5}", text)
res1 = re.match("\w{6,8}", text1)
res2 = re.match("\w{20,25}", text2)
 
print(res.group())
print(res1.group())
print(res2)

hello
Hellowor
None

Process finished with exit code 0

到此这篇关于python正则表达式re.match()匹配多个字符方法的实现的文章就介绍到这了,更多相关python re.match()匹配字符内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python二叉树遍历的实现方法
Nov 21 Python
Python random模块(获取随机数)常用方法和使用例子
May 13 Python
Python对list列表结构中的值进行去重的方法总结
May 07 Python
小议Python中自定义函数的可变参数的使用及注意点
Jun 21 Python
python网络爬虫之如何伪装逃过反爬虫程序的方法
Nov 23 Python
python 将大文件切分为多个小文件的实例
Jan 14 Python
Python批量查询关键词微信指数实例方法
Jun 27 Python
python下PyGame的下载与安装过程及遇到问题
Aug 04 Python
python str字符串转uuid实例
Mar 03 Python
python实现单张图像拼接与批量图片拼接
Mar 23 Python
详解向scrapy中的spider传递参数的几种方法(2种)
Sep 28 Python
python中os.remove()用法及注意事项
Jan 31 Python
python工具快速为音视频自动生成字幕(使用说明)
Jan 27 #Python
详解appium自动化测试工具(monitor、uiautomatorviewer)
Jan 27 #Python
Python利用socket模块开发简单的端口扫描工具的实现
Jan 27 #Python
Python爬虫实现selenium处理iframe作用域问题
Jan 27 #Python
python利用appium实现手机APP自动化的示例
Jan 26 #Python
python 基于opencv去除图片阴影
Jan 26 #Python
python中用ggplot绘制画图实例讲解
Jan 26 #Python
You might like
利用PHP实现与ASP Banner组件相似的类
2006/10/09 PHP
通俗易懂的php防注入代码
2010/04/07 PHP
php通过ajax实现双击table修改内容
2014/04/28 PHP
利用PHP绘图函数实现简单验证码功能的方法
2016/10/18 PHP
php 读取文件夹下所有图片、文件的实例
2018/10/17 PHP
解决 firefox 不支持 document.all的方法
2007/03/12 Javascript
用JS操作FRAME中的IFRAME及其内容的实现代码
2008/07/26 Javascript
javascript Select标记中options操作方法集合
2008/10/22 Javascript
JavaScript访问CSS属性的几种方式介绍
2014/07/21 Javascript
JavaScript中的函数重载深入理解
2014/08/04 Javascript
jsMind通过鼠标拖拽的方式调整节点位置
2015/04/13 Javascript
通过点击jqgrid表格弹出需要的表格数据
2015/12/02 Javascript
基于jQuery实现美观且实用的倒计时实例代码
2015/12/30 Javascript
基于MVC5和Bootstrap的jQuery TreeView树形控件(二)之数据支持json字符串、list集合
2016/08/11 Javascript
js实现产品缩略图效果
2017/03/10 Javascript
JS回调函数基本定义与用法实例分析
2017/05/24 Javascript
jQuery选择器之子元素过滤选择器
2017/09/28 jQuery
vue3.0 CLI - 2.3 - 组件 home.vue 中学习指令和绑定
2018/09/14 Javascript
JS前端知识点offset,scroll,client,冒泡,事件对象的应用整理总结
2019/06/27 Javascript
Vue触发input选取文件点击事件操作
2020/08/07 Javascript
[05:34]2014DOTA2国际邀请赛中国区预选赛精彩TOPPLAY第二弹
2014/06/25 DOTA
[59:35]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第二局
2016/03/04 DOTA
Python中对列表排序实例
2015/01/04 Python
用Python计算三角函数之acos()方法的使用
2015/05/15 Python
Python 通过URL打开图片实例详解
2017/06/01 Python
基于PyQt4和PySide实现输入对话框效果
2019/02/27 Python
python发送多人邮件没有展示收件人问题的解决方法
2019/06/21 Python
仪器仪表检测毕业生自荐信
2013/10/31 职场文书
大学优秀班集体申报材料
2014/05/23 职场文书
学习心理学的体会
2014/11/07 职场文书
模范班主任事迹材料
2014/12/17 职场文书
公务员政审材料范文
2014/12/23 职场文书
小学生学习保证书
2015/02/26 职场文书
2015年高一班主任工作总结
2015/05/13 职场文书
Python控制台输出俄罗斯方块移动和旋转功能
2021/04/18 Python
MySQL令人大跌眼镜的隐式转换
2021/08/23 MySQL